android取父控制項
① 什麼是android父控制項、子控制項,還有兩個有什麼關系
首先需要明白什麼是控制項?即xml中直接拖拽到布局的可視化「東西」
如下代碼:
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_background">
<TextView
android:id="@+id/version_detail_git"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@color/color_gray_bfc2c5"
android:textSize="15sp"/>
</RelativeLayout>- 上述代碼中,RelativeLayout是TextView的父控制項,TextView是RelativeLayout的子控制項,父控制項包含子控制項,然後在父控制項中調整對應的位置
② android如何獲得組件的父容器
Android中的每一個Activity都是有或多或少的view組成的,如果view沒有層級和歸屬,每個view相互獨立。那麼管理起來就會很麻煩,於是有了view層級的概念,也就是子布局,父容器。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--人數顯示和刷新按鈕-->
<include layout="@layout/anfrag_title" />
<!--時間選擇器-->
<include layout="@layout/anfrag_time_selector" />
<!--所有新增用戶圖表顯示-->
<include layout="@layout/new_total_user_item" />
<!--新增付費用戶圖表顯示-->
<include layout="@layout/new_vip_user_item" />
<!--新增免費用戶圖表顯示-->
<include layout="@layout/new_free_user_item" />
<!--新增用戶平台付費率圖表顯示-->
<include layout="@layout/new_pay_percent_item" />
</LinearLayout>
如上所述,LinearLayout就是相對的include的layout的父容器。
③ android自定義view如何獲取父容器賜予的寬度和高度
自定義View,想要自定義給定寬和高,你要寫自定義屬性,然後在xml文件中指定寬高才會有效,同時當給定的寬和高的值是wrap_content 或 fill_parent 這類的,這時需要在自定義View中重寫onMeasure方法,進行控制項的寬高測量。