📜  Android中的LinearLayout及其重要属性以及示例

📅  最后修改于: 2021-05-09 18:29:28             🧑  作者: Mango

LinearLayout是android studio中最基本的布局,它通过指定android:orientation属性以水平或垂直方式顺序对齐所有子级。如果应用android:orientation =“ vertical”,则元素将以垂直方式一个接一个地排列;如果应用android:orientation =“ horizontal”,则元素将以水平方式一个接一个地排列。

LinearLayout的示例代码

XML



XML


    
  
    
    


XML


    
  
    
    


XML


    
    
  
    
    
    


XML


    
    
  
    
    
    


XML


    
  
    
    
    


LinearLayout的一些重要属性

Attributes

Description

android:layout_weight

It is defined individually to the child’s views to specify how LinearLayout 

divides the remaining space amongst the views it contains

android:weightSum Defines the total weight sum
android:orientation How the elements should be arranged in the layout. It can be horizontal or vertical.
android:gravity

It specifies how an object should position its content on its X and Y axes. 

Possible values are – center_vertical, fill, center, bottom, end, etc.

android:layout_gravity

Sets the gravity of the View or Layout relative to its parent. 

Possible values are – center_vertical, fill, center, bottom, end, etc.

android:baselineAligned

This must be a boolean value, either “true” or “false” and prevents the layout 

from aligning its children’s baselines.

android:id This gives a unique id to the layout.

例子

1.如何以垂直方式安排儿童观

XML格式



    
  
    
    

输出界面:

上面代码的输出

2.如何横向排列儿童视野

XML格式



    
  
    
    

输出界面:

上面代码的输出

3.如何使用layout_weight和weightSum

XML格式



    
    
  
    
    
    

输出界面:

上面代码的输出

4.如何使用重力

XML格式



    
    
  
    
    
    

输出界面:

上面代码的输出

5.如何使用layout_gravity

XML格式



    
  
    
    
    

输出界面:

上面代码的输出

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!