为了创建比例大小的子View,可以将LinearLayout的宽度和高度设为fill_parent, 而将子View的宽度或是高度设为0,然后为子View设置不同权重(weight) ,这样子View的大小就会权值成比例。

本例使用横向LinearLayout,LinearLayout的android:layout_width=”match_parent”,表示将使用整个屏幕宽度。

对于LinearLayout的几个子View,将它们的宽度都定义为0,android:layout_width=”0dip”,然后使用layout_weight 为每个View指定宽度比例,本例为每个TextView都使用了相同的权值,因此四个TextView将会有相同的宽度。这样对于那些显示文字比较长的TextView的高度就变为多行。

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

<TextView
android:background=”@drawable/red”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:text=”@string/linear_layout_7_small”/>

<TextView
android:background=”@drawable/green”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:text=”@string/linear_layout_7_big”/>

<TextView
android:background=”@drawable/blue”
android:layout_width=”0dip”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:text=”@string/linear_layout_7_small” />

<TextView
android:background=”@drawable/yellow”
android:layout_width=”0dip”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:text=”@string/linear_layout_7_wrap”/>

</LinearLayout>

更多相关文章

  1. [Android] 获取系统顶部状态栏(Status Bar)与底部导航栏(Navigat
  2. Android通知栏的高度获取
  3. Android获取系统顶部状态栏(Status Bar)与底部导航栏(Navigation
  4. android 获取屏幕的宽度和高度
  5. Android Studio——Android获取屏幕宽度的4种方法
  6. android 聊天用RecyclerView获取键盘高度和消息自动显示在最底部

随机推荐

  1. Androidx学习笔记(7)--常见布局--相对布局
  2. Android(安卓)5.X新特性——Material Des
  3. Android中对Handler用法的总结
  4. Android:apk文件结构及打包技巧
  5. android开发学习大纲
  6. Android经典的大牛博客推荐
  7. android:使用Gon解析解析json
  8. Android(安卓)java.io.IOException异常情
  9. android初次学习(android studio安装及注
  10. Android:onTouch()和onTouchEvent()的区