亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android超詳細(xì)講解組件LinearLayout的使用

 更新時(shí)間:2022年03月31日 17:09:54   作者:小皮豬  
LinearLayout又稱作線性布局,是一種非常常用的布局。正如它的名字所描述的一樣,這個(gè)布局會(huì)將它所包含的控件在線性方向上依次排列。既然是線性排列,肯定就不僅只有一個(gè)方向,這里一般只有兩個(gè)方向:水平方向和垂直方向

概述

LinearLayout是線性布局組件,放置在其中的組件按列或者按行(就是垂直或者水平)的方式排序分布。

常用XML配置屬性

(1) android:orientation

設(shè)置LinearLayout容器布局組件的方式:只能取值:horizontal(水平的),vertical(垂直的)

(2) android:gravity

設(shè)置布局在LinearLayout容器內(nèi)的組件對(duì)齊方式。

取值包括top, bottom, left, right, center, start, end(上,下,左,右,中,開始,結(jié)束)

(3) View中繼承來的屬性

(包括android:background ,android:visibility等。還有一些改善美觀的放置組件的間隔)

1. android:layout_width和android:layout_height (match_parent/wrap_content)

2 .android:layout_gravity   設(shè)置組件在容器中的布局 

3. android:layout_weight  設(shè)置組件占用空間的空余顯示空間的比列

4. android:layout_margin  ,android:layout_marginTop  ,android:layout_marginBottom  ,android:layout_marginLeft  ,android:layout_marginRight 設(shè)置組件的外邊界,類似我們搞網(wǎng)頁設(shè)計(jì)HTML/CSS中margin用法。

代碼舉例

activity_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"
    android:orientation="vertical">
    <RadioGroup
        android:id="@+id/orientation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        >
        <RadioButton
            android:id="@+id/horizontal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="水平"
            android:textSize="30dp"
            />
        <RadioButton
            android:id="@+id/vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="垂直"
            android:textSize="30dp"
            />
    </RadioGroup>
 
    <RadioGroup
        android:id="@+id/gravity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp"
        >
        <RadioButton
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="居左"
            android:textSize="30dp"
            />
        <RadioButton
            android:id="@+id/center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="居中"
            android:textSize="30dp"
            />
        <RadioButton
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="居右"
            android:textSize="30dp"
            />
 
 
 
    </RadioGroup>
 
</LinearLayout>

MainActivity.java

package com.example.android_demo02;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
 
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup orientation;
    private RadioGroup gravity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        orientation=(RadioGroup) findViewById(R.id.orientation);
        orientation.setOnCheckedChangeListener(this);
        gravity=(RadioGroup) findViewById(R.id.gravity);
        gravity.setOnCheckedChangeListener(this);
    }
 
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.horizontal:
                orientation.setOrientation(LinearLayout.HORIZONTAL);
                break;
            case R.id.vertical:
                orientation.setOrientation(LinearLayout.VERTICAL);
                break;
            case R.id.left:
                gravity.setGravity(Gravity.START);
                break;
            case R.id.center:
                gravity.setGravity(Gravity.CENTER_HORIZONTAL);
                break;
            case R.id.right:
                gravity.setGravity(Gravity.END);
                break;
        }
    }
 
}

實(shí)現(xiàn)效果:

到此這篇關(guān)于Android超詳細(xì)講解組件LinearLayout的使用的文章就介紹到這了,更多相關(guān)Android LinearLayout內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論