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>


标签:layout,布局
0
投稿

猜你喜欢

  • C++异常处理 try,catch,throw,finally的用法

    2021-08-21 21:29:55
  • Java输入输出流的使用详细介绍

    2023-08-01 22:21:22
  • Android Kotlin使用SQLite案例详解

    2022-11-01 19:20:37
  • Java中的字节流文件读取教程(一)

    2023-08-14 08:14:47
  • Java游戏俄罗斯方块的实现实例

    2022-11-26 12:35:49
  • Java实现天天酷跑小游戏完整代码(附源码)

    2021-08-03 09:43:43
  • Spring声明式事务和@Aspect的拦截顺序问题的解决

    2023-07-18 12:10:46
  • 深入理解spring boot 监控

    2023-11-04 02:25:15
  • Winform 控件优化LayeredWindow无锯齿圆角窗体

    2021-12-07 22:54:17
  • 最好的8个Java RESTful框架

    2023-02-07 07:49:44
  • Java中ClassLoader类加载学习总结

    2022-08-26 18:13:42
  • SpringMVC+Mybatis实现的Mysql分页数据查询的示例

    2023-11-24 20:53:33
  • Java使用Scanner类进行控制台输入实现方法

    2021-08-08 18:34:26
  • 手把手教你SpringBoot轻松整合Minio

    2022-07-24 14:00:37
  • Android自定义attr的各种坑

    2023-07-12 06:11:50
  • 详解Android Service与Activity之间通信的几种方式

    2023-10-26 04:15:48
  • C#中事件只能在内部调用的原因分析

    2023-08-09 05:07:00
  • Java设计模式之工厂模式

    2023-12-18 01:40:50
  • Java实现马踏棋盘算法

    2023-03-05 04:30:46
  • JAVA设置手动提交事务,回滚事务,提交事务的操作

    2022-07-20 08:07:40
  • asp之家 软件编程 m.aspxhome.com