android:baselineAligned,baselineAlignedChildIndex,addStatesFromChildren

对于可以显示文字的View(如TextView,Button等),它的baseline 指的是这个UI控件中文字Text的baseline 到UI控件顶端的偏移值

可以理解为text下面那条看不见的线

如图设置baseline对齐和不对齐的效果, 左边baselineAligned=true, 右边baselineAligned=false, 红线为假想的baseline

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="3dip"
        android:layout_gravity="center_vertical"
        android:text="@string/baseline_nested_1_label" />

    <!-- We want the middle textview of this vertical linear layout to
      be baseline aligned with the others.-->
    <LinearLayout
            android:orientation="vertical"
            android:baselineAlignedChildIndex="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="http://fqj183-8041380.blog.163.com/blog/@drawable/arrow_up_float"/>
       1: <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:text="@string/baseline_nested_1_label" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="http://fqj183-8041380.blog.163.com/blog/@drawable/arrow_down_float"/>
    </LinearLayout>


    <!-- We want the third index textview of this vertical linear layout to
      be baseline aligned with the others.-->
    <LinearLayout
            android:orientation="vertical"
            android:baselineAlignedChildIndex="2"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="http://fqj183-8041380.blog.163.com/blog/@drawable/arrow_up_float"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="http://fqj183-8041380.blog.163.com/blog/@drawable/arrow_up_float"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:text="@string/baseline_nested_1_label" />
    </LinearLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_gravity="center_vertical"
        android:text="@string/baseline_nested_1_label" />


</LinearLayout>

红线部分是指用这个layout下面的第几个元素跟其他的元素进行baseline

比方说第一个红线部分:是让这个layout下面的第一个元素,也就是1:处的textView,跟其他的元素进行baseline,这个元素设置了:android:layout_marginRight="5dip",右边 的linearlayout离着这个元素的右边是5dip.

android:layout_baselinealignedChildIndex属性 指当前layout是 以哪个view的基准线与其他的View进行对齐。这个属性只对Button、TextView、EditView有效且下标从0开始。

android:addStatesFromChildren="true".属性说明该viewgroup的drawable属性是否把它的子类的drawable的state包含进来。测试中linearlayout如果不包含该属性(false),当子widget被点击时不会出现被选中的状态。也就是子类的state不会被传递给父类了

猜你喜欢

转载自2528.iteye.com/blog/2020901