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

关于weight的使用,因为weight在布局中主要按比例进行组件的摆放,因此比較easy解决适配的问题,所以学会使用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>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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

xml布局内容总结(一)--Android的更多相关文章

  1. xml布局内容总结(四)--Android

    (1)对于xml编写界面较复杂的情况下,使用include会使得编写和查看更清楚 <LinearLayout             android:layout_width="mat ...

  2. xml布局内容总结(三)--Android

    关于xml中经经常使用到边框及边框效果,在此进行一下总结. 3.border(边框及边框效果) (1)直角边框线 <LinearLayout         android:layout_wid ...

  3. 通过在xml布局文件中设置android:onClick=""来实现组件单击事件

    在布局中出现android:onClick=""语句: <Button android:id="@+id/call_button" android:onC ...

  4. android xml布局文件属性说明

    android xml布局文件属性说明 [摘]android xml布局文件属性说明 LinearLayout和RelativeLayout 共有属性:java代码中通过btn1关联次控件androi ...

  5. android 开发 使用自定义布局实现标题栏复用(标题栏内容自定义:使用代码实现和xml布局自定义属性2种办法实现)

    在个人学习的情况下可能很少使用自定义布局去实现大量复用的情况下,但是在一个开发工作的环境下就会使用到大量复用的自定义控件. 实现思维: 1.写一个xml的布局,用于标题栏的样式,并且添加在标题栏中你想 ...

  6. Android中将xml布局文件转化为View树的过程分析(下)-- LayoutInflater源码分析

    在Android开发中为了inflate一个布局文件,大体有2种方式,如下所示: // 1. get a instance of LayoutInflater, then do whatever yo ...

  7. Android开发 ---xml布局元素

    1.android:orientation="vertical/horizontal" vertical为垂直布局, horizontal为水平布局 2.android:layou ...

  8. Android中measure过程、WRAP_CONTENT详解以及 xml布局文件解析流程浅析

    转自:http://www.uml.org.cn/mobiledev/201211221.asp 今天,我着重讲解下如下三个内容: measure过程 WRAP_CONTENT.MATCH_PAREN ...

  9. Android Fragment之间的通信(用fragment替换掉XML布局文件中的一个线性布局)

    1.XML布局 (1)主界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...

随机推荐

  1. Maven下载安装步骤

    Maven下载安装步骤 1.下载maven 进入Maven官网的下载页面:http://maven.apache.org/download.cgi,如下图所示: 选择当前最新版本:"apac ...

  2. VideoView视频缓冲进度条

    效果图: 需求: 刚进入视频播放页时,屏幕中间有加载进度条 视频播放过程中,视频界面不动了,正在缓冲时,屏幕中间有加载进度条 private ObjectAnimator rotate; ImageV ...

  3. jquery DOM操作(一)

    上一篇文章是记录了jquery中选择器的作用,这里只要记录下jquery中的DOM操作,还是按照上篇的格式来. 下面是测试用的html代码,接下来DOM的操作会在下面的代码下进行. <body& ...

  4. jQuery.Validate.js验证大表单的优化

    最近在项目中有遇到一个Form表单中有200多个标签.在提交表单时网页会出现等待时间很长,甚至会出现网页奔溃的情况. 主要的原因是因为在使用jQuery.Validate.js进行Form验证的时候会 ...

  5. 最短路——spfa

    适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...

  6. PlayMaker GUI跟随布局的使用

    PlayMaker GUI跟随布局的使用   PlayMaker提供一种的特殊的GUI布局方式GUI Layout (Begin) Area Follow Object.这种布局可以在特定游戏对象上显 ...

  7. Inno Setup Winfrom 打包工具

    1.下载并安装Inno Setup 编译器 2.安装完成后打开Inno Setup 编译器: 3.选择[用「脚本向导」创建新的脚本文件(S)]: 4.点击[下一步]: 5.填写制作后程序的基本信息: ...

  8. HDU2874【倍增、ST】

    题目链接[https://vjudge.net/problem/HDU-2874] 题意: 输入一个森林,总节点不超过N(N<10000),由C次询问(C<1000000),每次询问两个点 ...

  9. 【UOJ #105】【APIO2014】Beads and wires

    http://uoj.ac/problem/105 好神的dp啊. 确定一个点为根之后,蓝线只能是竖着的,不能横跨兄弟. 枚举每个点为根进行树形dp是\(O(n^2)\)的,\(f(x,0/1)\)表 ...

  10. FileZilla提权的过程

    话不多说,我们直接操作! 首先我们通过 webshell 找到 FileZilla 的文件目录,找到 FileZilla Server Interface.xml 文件,打开.我们可以看到它的连接地址 ...