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方法,进行控件的宽高测量。