Android布局之LinearLayout

LinearLayout

1.核心属性

  高度:layout_height   (基于内容 wrap_content;基于父控件;)

  宽度:layout_width

  方向:orientation  (纵向 vertical;横向 horizontal;)

  位置:layout_gravity (居中 center;居右 right;)

2.等分控件

  做等分时将layout_width设为0dp layout_weight设为权重比是谷歌推荐的做法

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
    <!-- 横排等分 layout_width="match_parent" 根据父控件的宽度决定自己的宽度-->
    <LinearLayout
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <!-- 做等分时将layout_width设为0dp layout_weight设为权重比是谷歌推荐的做法-->
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#234578"
            android:text="等分1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#234578"
            android:text="等分1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="#284578"
            android:text="等分(1+1)"/>
    </LinearLayout>

    <!-- 横排非等分 layout_width="wrap_content" 根据内容的宽度决定自己的宽度-->
    <LinearLayout
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="10dp">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#897654"
            android:text="非等分1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#897654"
            android:text="=====非等分123====="/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:background="#897654"
            android:text="Demo"/>
    </LinearLayout>

    <!-- 横排等分 -->
    <LinearLayout
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_marginTop="10dp">

        <!-- 竖排等分 -->
        <LinearLayout
            android:orientation="vertical"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:background="#457421"
                android:text="等分1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#126735"
                android:text="等分2"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#843975"
                android:text="等分3"/>
        </LinearLayout>

        <!-- 竖排按数值确定高度 -->
        <LinearLayout
            android:orientation="vertical"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="20dp">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="44dp"
                android:background="#547836"
                android:text="按钮1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="66dp"
                android:background="#837282"
                android:text="按钮2"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="98dp"
                android:layout_weight="2"
                android:background="#973422"
                android:text="按钮3"/>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="right">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="44dp"
            android:background="#547836"
            android:text="靠右"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="44dp"
            android:background="#547836"
            android:text="居中"/>
    </LinearLayout>
</LinearLayout>

效果图

  

时间: 2024-08-13 10:34:41

Android布局之LinearLayout的相关文章

Android 布局之LinearLayout 子控件weight权重的作用详析

关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法二:值越大,重要性越低,所占用的空间越小. 到底哪个正确?哪个错误?抑或还有其他解释?请点击查看关于weight 权重参数作用的详分析: 其实这两种情况都不太准确: 准确的解释是,weight 权限 是用于分配父控件某一方向上尺寸-所有子控件在该方向上设定尺寸和 所得值的一个参数,把这个相减得到的结

Android 布局之LinearLayout 子控件weight权重的作用详析(转)

关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法二:值越大,重要性越低,所占用的空间越小. 到底哪个正确?哪个错误?抑或还有其他解释?请点击查看关于weight 权重参数作用的详分析: 其实这两种情况都不太准确: 准确的解释是,weight 权限 是用于分配父控件某一方向上尺寸-所有子控件在该方向上设定尺寸和 所得值的一个参数,把这个相减得到的结

Android布局中LinearLayout的android:layout_gravity=&quot;bottom&quot;不起作用

在做界面布局的时候,我们经常用到的就是LinearLayout,但是有时候用起来也不方便,比如我们在页面中要把一个Button放在屏幕底部,此时我们设置android:layout_gravity="bottom"是没有效果的.为什么呢?对于 LinearLayout: 当 android:orientation="vertical"  时, 只有水平方向的设置才起作用,垂直方向的设置不起作用.即:left,right,center_horizontal 是生效的.

android 布局之LinearLayout(学习一)

一,View localView = mRadioGroup_content.getChildAt(i);指定自定义菜单栏的点击格,如child3 其中:mRadioGroup_content = (LinearLayout) findViewById(R.id.mRadioGroup_content); 学习: ListView.getCount() 返回的所包含的item总个数 ListView.getChildCount() (ViewGroup.getChildCount())    返

Android布局及属性归总

原文地址:http://www.cnblogs.com/zyw-205520/archive/2013/01/18/2866575.html 常见布局 LinearLayout    线性布局        子元素任意,组织成一个单一的水平或垂直行,默认为水平方向TableLayout    表格布局        子元素为<TableRow>,一个TableRow就代表TableLayout中的一行RelativeLayout  相对布局        子元素任意AbsoluteLayout

Android的学习第六章(布局一LinearLayout)

今天我们来说一下Android五大布局-LinearLayout布局(线性布局) 含义:线性布局,顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的, 主要作用:主要对整个界面进行基本设置使用 重要属性:android:orientation 值:horizontal 元素水平摆放  |  vertical 元素垂直摆放 看代码: <!-- 第一个线性布局, 我们可以视为html中的div,用于对于整个界面进行布局 这里面 xmlns:android和xmlns:tool

Android布局之线性布局——LinearLayout

本文将详细介绍线性布局的各种xml属性. xml属性 <?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="mat

android基础之LinearLayout布局

LinearLayout布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="matc

Android五种布局方式——LinearLayout、RelativeLayout、TableLayout....(四)

Android五种布局方式--LinearLayout.RelativeLayout .TableLayout.... Android使用XML声明界面布局 将程序的表现层和控制层分离 修改用户界面时,无需更改程序的源代码 可视化工具设计用户界面 Android五种布局方式 LinearLayout线性布局 AbsoluteLayout坐标布局 RelativeLayout相对布局 FrameLayout帧布局 TableLayout表格布局 GridLayout 1.LinearLayout线