zoukankan      html  css  js  c++  java
  • Android布局_布局概述和LinearLayout布局

     一、布局概述:

      布局为UI提供了一个可视化的结构,比如对于一个activity或者app widget的UI,你可以用两种方式声明布局:

          在XML中声明UI元素        

          在运行时实例化布局元素(在java代码中去写布局)

      布局之间可以相互嵌套

          

    二、LinearLayout概述:

      LinearLayout是线性布局控件,它包含的子控件将横向或竖向的方式排列

          

    三、LinearLayout属性:

      1、android:orientation = ""  

              该属性决定他子类控件的排布方式(vertical:垂直;horizontal:水平)

      2、android:gravity = ""

            该属性决定他所有子类的xy的位置,多个属性可以一起用(android:gravity="bottom|right"),常用到的几个属性值:

              center_vertical:垂直(Y抽居中)

              center_horizontal:水平(X抽居中)

              center:水平垂直都居中

              right:子类控件位于当前布局的右边

              left:子类控件位于当前布局的左边

              bottom:子类控件位于当前布局的下面

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="bottom|right" >
        
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
    </LinearLayout>

      3、子类控件在LinearLayout中常用到的属性

        android:layout_gravity = "bottom"  ------指本身在当前父容器的XY的一个位置,这个属性注意的地方:

            这个属性的属性值跟android:gravity = ""是一样的

            有时候这个属性达不到你想要的效果,那么就用布局中的android:gravity = ""去控制

            需要结合线性布局中的android:orientation="vertical"属性一起使用,这个属性的值不同会有不同的效果

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <Button
            android:layout_gravity="center_horizontal"
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
    </LinearLayout>

        android:layout_weight = "1" -------指本身控件占当前父容器的一个比例

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_weight="2" />
        
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_weight="1" />
    
    </LinearLayout>

    四、布局的嵌套

      布局之间可以嵌套,就是一个布局里面可以在加多一个或多个布局(任意布局都可以,线性布局也可以嵌套相对布局)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"/>
        
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button"
                android:layout_weight="2" />
    
            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button"
                android:layout_weight="1" />
    
        </LinearLayout>
    
    </LinearLayout>
  • 相关阅读:
    Python调用R语言
    走迷宫(用栈模拟实现非递归,并输出路径)
    走迷宫(用栈模拟实现非递归,并输出路径)
    《Python数据可视化编程实战》
    《Python数据可视化编程实战》
    一道思考题(二进制枚举的应用的想法)切金条
    Android 自己定义UI圆角button
    Oracle 用户管理(二)
    最大团解析及用处
    用反射完毕学生管理系统(包含数据库连接)
  • 原文地址:https://www.cnblogs.com/LO-ME/p/4678914.html
Copyright © 2011-2022 走看看