LinearLayout中的属性baselineAligned的使用

本文详细解析了Android线性布局中baselineAlignedChildIndex属性的作用,通过实例展示了如何利用此属性实现文字对齐的效果。文章还提供了XML代码示例,直观地展示了属性的使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Android线性布局中的属性主要的就是控制浮动方向的orientation,其他的就是辅助浮动显示的,其中有一个属性控制基线,也就是baselineAligned,让我有点迷惑,下边通过例子讲解下这个属性的使用。

1.首先这个基线主要是对可以显示文字的View,如TextView,Button等控件的

2.这个baseline指的是这个UI控件的baseline--文字距UI控件顶部的偏移量

3.LinearLayout控件默认有属性android:baselineAligned为true,如果LinearLayout的orientation为horizontal的话,其中的文字默认是文字对齐的

下边举个例子看下,效果和代码如下:


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:baselineAlignedChildIndex="3"
  6. android:orientation="horizontal" >
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_marginRight="3dip"
  11. android:text="String1" />
  12. <LinearLayout
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:baselineAlignedChildIndex="1"
  16. android:orientation="vertical" >
  17. <ImageView
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:src="@android:drawable/arrow_up_float" />
  21. <TextView
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginRight="5dip"
  25. android:text="String2" />
  26. <ImageView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:src="@android:drawable/arrow_down_float" />
  30. </LinearLayout>
  31. <LinearLayout
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:baselineAligned="true"
  35. android:baselineAlignedChildIndex="2"
  36. android:orientation="vertical" >
  37. <ImageView
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:src="@android:drawable/arrow_up_float" />
  41. <ImageView
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:src="@android:drawable/arrow_down_float" />
  45. <TextView
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_marginRight="5dip"
  49. android:text="String3" />
  50. </LinearLayout>
  51. <TextView
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:text="String4"
  55. android:textSize="60sp" />
  56. </LinearLayout>

其中的baselineAlignedChildIndex指的是其中的第几个子控件按照baseline对齐的。

原地址:http://txlong-onz.iteye.com/blog/1961170

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值