LinearLayout 可以嵌套使用,当一个LinearLayout作为其它Layout子类时,可以使用android:baselineAlignedChildIndex 指定该LinearLayout的哪个子类需要和其它支持baseline的UI控件对齐。

android:baselineAlignedChildIndex 值必须为整数,为子View的序号(从0开始)。

本例 和下面几个 Nested Example ,Google提供的示例是不正确的,根本就没有能够演示android:baselineAlignedChildIndex 的用法. 这是因为LinearLayout 使用了center_vertical的对齐方式,此时baseline 对齐就不起作用了。参见Android ApiDemos示例解析(135):Views->Layouts->Baseline->3. Center。

修改baseline_nested_1.xml 去掉所有的android:layout_gravity=”center_vertical” ,修改后的XML如下:

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”
android:gravity=”center_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=”@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” />
<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@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=”@drawable/arrow_up_float”/>
<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@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>

本例嵌套了两个子LinearLayout ,第一个将android:baselineAlignedChildIndex 设为1,第二个将android:baselineAlignedChildIndex 设为2 ,都将子LinearLayout 中的TextView和其它支持baseline对齐的UI(如TextView) 实现文字baseline 对齐。

Android ApiDemos示例解析(139):Views->Layouts->Baseline->Nested Example 1_第1张图片

更多相关文章

  1. Android 事件分发详解及示例代码
  2. Android中Bundle的使用示例
  3. Android开发菜单布局之表格布局示例
  4. Android ndk 开发环境搭建 和 示例
  5. RecyclerView嵌套ScrollView
  6. Android情景模式、文件管理器 完整示例编程详解
  7. Android平台调用Web Service:示例
  8. Android 管理和组织首选项 (Preference) 附示例

随机推荐

  1. Android(安卓)学习笔记之界面布局
  2. Android第四讲——常用的基本控件(三)Toa
  3. Android(安卓)稀奇古怪的问题
  4. Android事件总线(二)EventBus3.0源码解析
  5. Android进阶(三)ButterKnife源码解析
  6. android studio 3.0 preview 使用kotlin
  7. 读书笔记——《Android(安卓)开发艺术探
  8. Android(安卓)RoboGuice 使用指南(14):In
  9. Android(安卓)双屏异显
  10. android手机连接到ubuntu方法