xml布局内容总结一--Android

关于安卓项目中xml的使用非常多,为了达到一些好的UI效果,需要对xml比较熟练,会使用很多的小技巧,本人准备对这些小技巧进行整理和总结,希望进行分享和交流。

关于weight的使用,由于weight在布局中主要按比例进行组件的摆放,因此比较容易解决适配的问题,所以学会使用weight会减轻开发过程中部分繁琐的开发任务。

1.使用weight居中显示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="1"
>
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Weight_Button" />
</LinearLayout>



2.使用weight实现相同宽度的n个组件(这里用3个做测试)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
>


<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight_Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight_Button2" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight_Button3" />


</LinearLayout>




3.使用weight实现不同比例宽度的n个组件(这里用4个做测试)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10"
>


<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="4"
android:background="@color/yellow"
android:text="4" />
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="3"
android:background="@color/red"
android:text="3" />
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="2"
android:background="@color/yellow"
android:text="2" />
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@color/red"
android:text="1" />


</LinearLayout>


占据比例分别为总宽度的0.4,0.3,0.2,0.1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇