日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android常用布局-01

發(fā)布時(shí)間:2023/12/20 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android常用布局-01 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android中常用的5大布局方式有以下幾種:

  • 線性布局(LinearLayout):按照垂直或者水平方向布局的組件。
  • 幀布局(FrameLayout):組件從屏幕左上方布局組件。
  • 表格布局(TableLayout):按照行列方式布局組件。
  • 相對(duì)布局(RelativeLayout):相對(duì)其它組件的布局方式。
  • ?絕對(duì)布局(AbsoluteLayout):按照絕對(duì)坐標(biāo)來布局組件

1. 線性布局
線性布局會(huì)將其中的控件一個(gè)接一個(gè)排序,可以橫排也可豎排


常用的屬性:

android:orientation:可以設(shè)置布局的方向
android:gravity:用來控制組件的對(duì)齊方式
layout_weight:控制各個(gè)組件在布局中的相對(duì)大小

第一個(gè)實(shí)例

①效果圖:


②核心代碼如下:

<?xml version="1.0" encoding="utf-8"?> <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="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.a14284.myapplication.MainActivity"><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" ><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="左上按鈕" /><Button android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="8" android:text="1" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="右上按鈕" /></LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" ><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="中間按鈕" /></LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" ><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="bottom" ><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="左下按鈕" /><Button android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="8" android:text="1" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="右下按鈕" /></LinearLayout></LinearLayout> </LinearLayout>

第二個(gè)實(shí)例

①效果圖:

?



②核心代碼:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1,2" android:shrinkColumns="0" ><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="這是第一個(gè)lay的第一行" android:background="#AD0000" /><TableRow><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2行1列" android:background="#AD0000" /><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2行2列" android:background="" /><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2行3列" android:background="#AD00AD" /></TableRow></TableLayout><TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:collapseColumns="1" ><TableRow><TextView android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="第二個(gè)layout第1行1列" android:background="#21FF00" /><TextView android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="第二個(gè)layout第1行2列" android:background="#21FF00" /><TextView android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="第二個(gè)layout第1行3列" android:background="#9C00FF" /></TableRow><TableRow><TextView android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="第2行1列" android:background="#21FF00" /><TextView android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="第2行2列" android:background="#21FF00" /><TextView android:layout_weight="wrap_content" android:layout_height="wrap_content" android:text="第2行3列" android:background="#9C00FF" /></TableRow></TableLayout> </LinearLayout>2.?幀布局幀布局是從屏幕的左上角(0,0)坐標(biāo)開始布局,多個(gè)組件層疊排列,第一個(gè)添加的組件放到最底層,最后添加到框架中的視圖顯示在最上面。上一層的會(huì)覆蓋下一層的控件。簡(jiǎn)單的例子①效果圖: ②?核心代碼:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/a1" /><ImageView android:layout_width="300dp" android:layout_height="300dp" android:src="@drawable/a2" android:layout_gravity="center" /><ImageView android:layout_width="250dp" android:layout_height="250dp" android:src="@drawable/a3" android:layout_gravity="center" /><ImageView android:layout_width="200dp" android:layout_height="200dp" android:src="@drawable/a4" android:layout_gravity="center" /><ImageView android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/a5" android:layout_gravity="center" /> </FrameLayout>3.表格布局表格布局是一個(gè)ViewGroup以表格顯示它的子視圖(view)元素,即行和列標(biāo)識(shí)一個(gè)視圖的位置。表格布局常用的屬性如下:android:collapseColumns:隱藏指定的列 android:shrinkColumns:收縮指定的列以適合屏幕,不會(huì)擠出屏幕 android:stretchColumns:盡量把指定的列填充空白部分 android:layout_column:控件放在指定的列 android:layout_span:該控件所跨越的列數(shù)?簡(jiǎn)單的列子:①效果圖:②?核心代碼: <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="3" android:columnCount="4" ><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-"/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="."/><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:layout_rowSpan="2" android:layout_gravity="fill_vertical" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" android:layout_columnSpan="3" android:layout_gravity="fill_horizontal"/> <Space /> </GridLayout>





總結(jié)

以上是生活随笔為你收集整理的Android常用布局-01的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。