ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android笔记——线性布局(LinearLayout)

2021-09-03 13:02:29  阅读:193  来源: 互联网

标签:layout 笔记 LinearLayout 宽度 20dp 组件 Android id android


线性布局常用属性:
android:id:设置id,可以通过id找到该控件
android:layout_margin:外边框
android:layout_padding:内边框
android:layout_width:宽度
android:layout_height:高度
android:background:背景
android:orientation:屏幕显示方向(horizontal水平、vertical垂直)

 

编写acrivity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lqh.myapplication.MainActivity">
    <LinearLayout
        android:id="@+id/linear_01"
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:background="#000000"
        android:orientation="vertical"
        android:paddingBottom="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="20dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0033" />
    </LinearLayout>
    <!--
        第一个LinearLayout布局宽度只占了父级的100dp,而第二个布局匹配了父级的剩余的宽度,所以两个布局加起来刚好通屏
    -->
    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="200dp"
        android:layout_marginLeft="10dp"
        android:background="#0066FF"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <View
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#FF0033" />

        <View
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#ffffff" />

        <View
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#ccc112" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">
<!--
    当多个组件有着同样的权重,但有些组件的宽或高有固定值时;这时,父级剩下的宽或高会分配给这些组件。
-->
        <View
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:background="#ffddee" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#aabbcc" />
    </LinearLayout>
</LinearLayout>

 

(1)@+id/id名:创建id
(2)match_parent:高度或宽度匹配父级;高度和宽度也可以是固定的,单位用dp
(3)android:background="#n数值":背景颜色不能直接写颜色,要写"#数值"的形式
(4)View:所有组件的父类,所以组件都继承view
(5)padding:内边距,可以单独设置四个方向(top、bottom、left、right)
android:padding="20dp"
相当于:
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
(6)layout_margin:外边距,也可以单独设置四个方向(top、bottom、left、right)
(7)android:gravity:设置内部元素的对齐方式
center:水平垂直居中
center_vertical:垂直居中
center_horizontal:水平居中
(8)android:layout_weight:权重
使多个组件占用父级宽度或高度都相同的方法:
1.在这些组件里面添加相同的layout_weight
2.高度或宽度设为0dp

标签:layout,笔记,LinearLayout,宽度,20dp,组件,Android,id,android
来源: https://www.cnblogs.com/lqh0904/p/15222615.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有