android layout 按比例布局的代码
2016-04-28 21:06
393 查看
突然发现这个很有用,所以转载了它。
android layout 按比例布局的代码,需要的朋友可以参考一下
为了创建比例大小的子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>
android layout 按比例布局的代码,需要的朋友可以参考一下
为了创建比例大小的子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>
相关文章推荐
- Android Activity使用拾遗
- 安卓基础(一),简单控件的简单应用
- Android Activity使用拾遗
- Android——onSaveInstanceState()
- Android配置ip地址
- Android 开发配置
- 20145215实验四 Android开发基础
- Android——Activity生命周期
- 图片加载的基本方式
- Android编程之SparseArray<E>详解[取代HashMap]
- [置顶] Android自定义控件
- Bitmap的存储发展
- AndroidManifest文件中的几个细节
- android中把文件存储在SD卡中
- Android最佳性能实践(三)——高性能编码优化
- Android Intent意图详解
- android横线和竖线
- Android最佳性能实践(二)——分析内存的使用情况
- android 读写txt文件
- Android 事件处理机制