android layout 按比例布局的代码
时间:2022-10-11 13:02:40
为了创建比例大小的子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>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C++异常处理 try,catch,throw,finally的用法
Java输入输出流的使用详细介绍
![](https://img.aspxhome.com/file/2023/0/81880_0s.jpg)
Android Kotlin使用SQLite案例详解
Java中的字节流文件读取教程(一)
![](https://img.aspxhome.com/file/2023/0/124710_0s.png)
Java游戏俄罗斯方块的实现实例
Java实现天天酷跑小游戏完整代码(附源码)
![](https://img.aspxhome.com/file/2023/3/60833_0s.png)
Spring声明式事务和@Aspect的拦截顺序问题的解决
深入理解spring boot 监控
![](https://img.aspxhome.com/file/2023/0/67490_0s.png)
Winform 控件优化LayeredWindow无锯齿圆角窗体
![](https://img.aspxhome.com/file/2023/4/119504_0s.jpg)
最好的8个Java RESTful框架
Java中ClassLoader类加载学习总结
![](https://img.aspxhome.com/file/2023/6/116226_0s.png)
SpringMVC+Mybatis实现的Mysql分页数据查询的示例
![](https://img.aspxhome.com/file/2023/2/58692_0s.png)
Java使用Scanner类进行控制台输入实现方法
手把手教你SpringBoot轻松整合Minio
Android自定义attr的各种坑
详解Android Service与Activity之间通信的几种方式
C#中事件只能在内部调用的原因分析
Java设计模式之工厂模式
![](https://img.aspxhome.com/file/2023/9/60339_0s.png)
Java实现马踏棋盘算法
![](https://img.aspxhome.com/file/2023/4/76254_0s.jpg)