关于安卓项目中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. Linux 基础——权限管理命令chown、chgrp

    一.chown命令与chgrp命令的作用 有时你需要改变文件或目录的属主,比如有人离职或开发人员创建了一个在测试或生产环境中需要归属在系统账户下的应用.Linux提供了两个命令来实现这个功能:chow ...

  2. 未能加载文件或程序集“System.Data.SQLite”

    未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误 ...

  3. ArrayList中重复元素处理方法.[Java]

    1.使用HashSet删除ArrayList中重复的元素 private static void sortByHashSet() { ArrayList<String> listWithD ...

  4. Good Bye 2014 E - New Year Domino 单调栈+倍增

    E - New Year Domino 思路:我用倍增写哒,离线可以不用倍增. #include<bits/stdc++.h> #define LL long long #define f ...

  5. bzoj 1112 treap树

    思路:我们只要check一遍每个长度为k的区间就好啦,对于一个区间来说的最优值显然是中位数,我们显然要动态求 第k大,所以需要一个二叉搜索树,用treap就好啦. #include<bits/s ...

  6. poj3414 Pots(BFS)

    题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...

  7. 《Android虚拟机》----虚拟机概述

    No1: 虚拟机是指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离的环境中的完整计算机系统. No2: Java虚拟机由如下五个部分组成:一组指令集.一组寄存器.一个栈.一个无用单元收集堆. ...

  8. 洛谷P4782 【模板】2-SAT问题 [2-SAT]

    题目传送门 [模板]2-SAT问题 题目背景 2-SAT 问题 模板 题目描述 有n个布尔变量 $x_1/~x_n$​ ,另有$m$个需要满足的条件,每个条件的形式都是“ $x_i$ 为$true/f ...

  9. 浅谈MVVM模式和MVP模式——Vue.js向

    浅谈MVVM模式和MVP模式--Vue.js向 传统前端开发的MVP模式 MVP开发模式的理解过程 首先代码分为三层: model层(数据层), presenter层(控制层/业务逻辑相关) view ...

  10. java getenv getProperties区别

    网上很多使用的是getProperties.说获得系统变量,但是其实不正确.getProperties中所谓的"system properties"其实是指"java s ...