zoukankan      html  css  js  c++  java
  • Android UI系列-----LinearLayout的综合使用

    这里将会对LinearLayout的布局方式进行一个综合的使用,通过一个例子来看看LinearLayout的嵌套布局方式,在这之前首先介绍三个属性:

    1.①android:layout_weigth:这个属性是用来控制子控件占据的比例的。如果一个父控件在摆放了子控件后,还有剩余空间,那么我们可以通过layout_weigth这个属性可以将剩余的空间按比例分配给子控件。注意layout_weight 瓜分的是父控件的剩余空间,而不是瓜分整个父控件

    例如:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AC8720"
        android:baselineAligned="false"
        android:orientation="horizontal"
        tools:context=".MainActivity" >
    
        <LinearLayout 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#ABCDEF"
            android:layout_weight="1"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:text="android"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:text="android"/>
        </LinearLayout>
        
        <LinearLayout 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#FEDCBA"
            android:layout_weight="1"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:text="java"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:text="java"/>
        </LinearLayout>
    
    </LinearLayout>

    小技巧:既然layout_weight瓜分的是父控件的剩余空间,那么我们如果要设置子控件占同样的空间比例的时候,可以将子控件的 layout_width 设置成 0dp,然后将layout_weigth设置成1,因为如果layout_width设置成0dp,则表示宽度为0,那么所有的空间都是剩余空间,然后设置layout_weigth=1,这样两个控件就占同样的空间了。即:

    android:layout_width="0dp"
    android:layout_weight="1"

    android:layout_gravity:这个属性是用来设置控件的放置位置的,例如居中放置,居左放置等。

    android:gravity:这个属性是用来设置控件中字体的放置位置的,同样可以设置居中、居左等。

    2.通过一个实例来熟悉一下LinearLayout的使用方法:

    布局文件的代码如下:

    <?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="vertical" >
        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="猜拳游戏"
            android:textSize="30sp"/>
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <LinearLayout 
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:layout_weight="1"
                android:orientation="vertical">
                <ImageView 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"
                    android:scaleType="centerCrop"/>
                <RadioGroup 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <RadioButton 
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="石头"/>
                    <RadioButton 
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="剪子"/>
                    <RadioButton 
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="布"/>
                </RadioGroup>
            </LinearLayout>
            <LinearLayout 
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:orientation="vertical">
                <ImageView 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher"
                    android:scaleType="centerCrop"/>
                <RadioGroup 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <RadioButton 
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="石头"/>
                    <RadioButton 
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="剪子"/>
                    <RadioButton 
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="布"/>
                </RadioGroup>
            </LinearLayout>
        </LinearLayout>
        <Button 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="确定"
            android:textSize="30sp"/>
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView 
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="结果"
                android:textSize="20sp"/>
            <TextView 
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="测试结果"
                android:textSize="20sp"/>
        </LinearLayout>
    
    </LinearLayout>
  • 相关阅读:
    shell脚本中判断上一个命令是否执行成功
    nginx 414 Request-URI Too Large
    nginx 重写URL尾部斜杠
    Linux shell 日期,时间相关的命令
    shell脚本中自定义日志记录到文件
    scanf后面跟一个getchar
    1.Tarball软件make与makefile详解(还需要补充)
    <>和“”的区别
    malloc,calloc,realloc,alloc
    toString()方法细节
  • 原文地址:https://www.cnblogs.com/xiaoluo501395377/p/3391203.html
Copyright © 2011-2022 走看看