Android线性布局

1. 概述:LinearLayout通过线性将容器一个一个排列起来
2.XML属性

xml属性 相关方法 作用
android:gratity setGravity 设置对齐方式,该属性支持多个属性值,可以用 竖线连接
android:orientation setOrientation 设置容器内组件的排列方向,水平(horizontal默认)或垂直(vertical)

3.案例

  • 对字符串资源Strings.xml
 <?xml version="1.0"  encoding="utf-8"?>
<resources>
      <string name="action_settings">Settings</string>
      <string name="hello_world">Hello world!</string>
      <string name="btn1">按钮1</string>
      <string name="btn2">按钮2</string>
      <string name="btn3">按钮3</string>
      <string name="btn4">按钮4</string>
      <string name="btn5">按钮5</string>

 </resources>
  • 对布局文件activity_main.xml重新编辑
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:gravity="center_horizontal"    à线性布局对齐方式
     android:orientation="vertical" >      à线性布局布局方向
    <Button

         android:id="@+id/btn1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/btn1" />
   
     <Button

         android:id="@+id/btn2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/btn2" />
   
     <Button

         android:id="@+id/btn3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/btn3" />
   
     <Button

         android:id="@+id/btn4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/btn4" />
   
     <Button

         android:id="@+id/btn5"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/btn5" />
   </LinearLayout> 

在这里插入图片描述

发布了15 篇原创文章 · 获赞 0 · 访问量 143

猜你喜欢

转载自blog.csdn.net/qq_44230959/article/details/105409843