Android開發(fā)中幾種布局方式介紹(磨礪營馬劍威)

今天我們的主要內(nèi)容就是安卓的主要幾個基礎(chǔ)的布局方式悲龟。(主要布局如下:)

1.線性布局(LinerLayout)

2.相對布局(RelativeLayout)

3.表格布局(TableLayout)

4.網(wǎng)格布局(GridLayout)

5.絕對布局(AbsoluteLayout)

6.幀布局(FrameLayout)

一:線性布局(LinerLayout)。

1.xml文件配置:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="5dp" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#0000FF"

android:gravity="center_horizontal|center_vertical"

android:text="線性布局"

android:textSize="20sp"/>

android:layout_width="200dp"

android:layout_height="150dp"

android:layout_gravity="center_horizontal|center_vertical"

android:background="#0000FF"

android:orientation="horizontal" >

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:background="#00FF00"

android:text="內(nèi)容一" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#F7F709"

android:text="內(nèi)容二" />

android:layout_width="200dp"

android:layout_height="150dp"

android:layout_gravity="right"

android:background="#0000FF"

android:orientation="horizontal" >

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#00FF00"

android:text="內(nèi)容一" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:background="#F7F709"

android:text="內(nèi)容二" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按一"

android:onClick="click"

/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按二"

android:onClick="click"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按三"

android:onClick="click"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按四"

android:onClick="click"/>

展示:

2.xml文件配置:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:gravity="center_horizontal|center_vertical"

android:text="線性布局登錄頁面"

android:textSize="20sp" />

android:layout_width="200dp"

android:layout_height="40dp"

android:layout_gravity="center_horizontal|center_vertical"

android:layout_marginTop="20dp"

android:orientation="horizontal" >

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="賬號:"

android:textSize="15sp" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="4" />

android:layout_width="200dp"

android:layout_height="40dp"

android:layout_gravity="center_horizontal|center_vertical"

android:orientation="horizontal" >

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="密碼:"

android:textSize="15sp" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="4" />

android:layout_width="200dp"

android:layout_height="30dp"

android:layout_gravity="center_horizontal|center_vertical"

android:layout_marginTop="15dp"

android:orientation="horizontal" >

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="記住賬號" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="記住密碼" />

android:layout_width="200dp"

android:layout_height="30dp"

android:layout_gravity="center_horizontal|center_vertical"

android:layout_marginTop="15dp"

android:orientation="horizontal" >

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:onClick="click"

android:text="登錄"

android:textSize="10sp" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="15sp" />

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:onClick="click"

android:text="注冊"

android:textSize="10sp" />

展示:

二:相對布局(RelativeLayout)

1.xml文件布局如下:

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:gravity="center"

android:text="相對布局登錄頁面"

android:textSize="20sp" />

android:id="@+id/textView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/textView"

android:layout_marginLeft="80px"

android:layout_marginTop="30dp"

android:text="賬號:"

android:textSize="15sp" />

android:id="@+id/firstEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignBottom="@id/textView1"

android:layout_marginLeft="130px"

android:layout_marginRight="50px" />

android:id="@+id/textView2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/firstEditText"

android:layout_marginLeft="80px"

android:layout_marginTop="20dp"

android:text="密碼:"

android:textSize="15sp" />

android:id="@+id/firstEditText2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignBottom="@id/textView2"

android:layout_marginLeft="130px"

android:layout_marginRight="50px" />

android:id="@+id/checkbox1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/firstEditText2"

android:layout_marginLeft="100px"

android:layout_marginTop="20dp"

android:text="記住賬號" />

android:id="@+id/checkbox2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignBottom="@id/checkbox1"

android:layout_marginLeft="280px"

android:layout_marginRight="50px"

android:text="記住密碼" />

android:id="@+id/cancelButton1"

android:layout_width="match_parent"

android:layout_height="40dp"

android:layout_below="@id/checkbox2"

android:layout_marginLeft="100px"

android:layout_marginRight="290px"

android:layout_marginTop="25dp"

android:text="登錄"

android:textSize="14sp" />

android:id="@+id/confremButton2"

android:layout_width="match_parent"

android:layout_height="40dp"

android:layout_alignBottom="@id/cancelButton1"

android:layout_marginLeft="290px"

android:layout_marginRight="100px"

android:text="注冊"

android:textSize="14sp" />

展示:

三:表格布局(TableLayout)

xml文件如下:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="5dp"

android:stretchColumns="2" >

android:layout_width="50dp"

android:layout_height="wrap_content"

android:gravity="center"

android:text="序號"

android:textSize="20sp" />

android:layout_width="60dp"

android:layout_height="wrap_content"

android:gravity="center"

android:text="書名"

android:textSize="20sp" />

android:gravity="center"

android:text="內(nèi)容"

android:textSize="20sp" />

android:layout_width="60dp"

android:layout_height="wrap_content"

android:gravity="center"

android:text="作者"

android:textSize="20sp" />

android:layout_height="1.5dp"

android:background="#00FF00" />

android:gravity="center"

android:text="1" />

android:gravity="center"

android:text="西游記" />

android:gravity="center"

android:text="取經(jīng)" />

android:gravity="center"

android:text="鄭晨" />

android:layout_height="1.5dp"

android:background="#00FF00" />

android:gravity="center"

android:text="2" />

android:layout_column="2"

android:gravity="center"

android:text="孫悟空大鬧天空" />

android:layout_height="1.5dp"

android:background="#00FF00" />

android:gravity="center"

android:text="3" />

android:layout_column="1"

android:gravity="center"

android:text="盤龍" />

android:layout_column="3"

android:gravity="center"

android:text="鄭晨" />

android:layout_height="1.5dp"

android:background="#00FF00" />

展示:

四:網(wǎng)格布局(GridLayout)

1.xml文件如下:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="5dp" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="30dp"

android:gravity="center"

android:text="計算機"

android:textSize="30sp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:layout_marginTop="20dp"

android:columnCount="4"

android:rowCount="6" >

android:id="@+id/textView6"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_columnSpan="4"

android:layout_gravity="fill_horizontal"

android:editable="false"

android:focusable="false"

android:gravity="right"

android:text="0"

android:textSize="20sp" />

android:onClick="acClick"/>

android:onClick="anClick" />

android:onClick="bnClick" />

android:onClick="onClick" />

android:layout_columnSpan="2"

android:layout_gravity="fill_horizontal"

android:text="0" />

2.Activity如下(這是我只寫了幾個按鍵可以按,作為例子):

//網(wǎng)格布局

public class GridLayout extends Activity {

private EditText editText;

private boolean lastClickIsNumber = false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.grid_layout);

editText = (EditText) findViewById(R.id.textView6);

}

public void acClick(View v) {

}

public void onClick(View v) {

if (!lastClickIsNumber) {

editText.setText("1");

} else {

String oldContent = editText.getText().toString().trim();

oldContent += "1";

editText.setText(oldContent);

}

lastClickIsNumber = true;

}

public void anClick(View v) {

if (!lastClickIsNumber) {

editText.setText("x");

} else {

String oldContent = editText.getText().toString().trim();

oldContent += "x";

editText.setText(oldContent);

}

lastClickIsNumber = true;

}

public void bnClick(View v) {

if (!lastClickIsNumber) {

editText.setText("6");

} else {

String oldContent = editText.getText().toString().trim();

oldContent += "6";

editText.setText(oldContent);

}

lastClickIsNumber = true;

}

}

展示:

五.絕對布局(AbsoluteLayout)

xml文件如下:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="5dp">

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_x="100dp"

android:layout_y="50dp"

android:text="你好嗎?"/>

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_x="50dp"

android:layout_y="100dp"

android:text="我很好!"/>

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_x="200dp"

android:layout_y="100dp"

android:text="真的嗎勘高?"/>

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_x="100dp"

android:layout_y="200dp"

android:text="真的!"/>

展示:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坟桅,一起剝皮案震驚了整個濱河市华望,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌仅乓,老刑警劉巖赖舟,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異夸楣,居然都是意外死亡宾抓,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進(jìn)店門豫喧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來石洗,“玉大人,你說我怎么就攤上這事紧显〗采溃” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵孵班,是天一觀的道長涉兽。 經(jīng)常有香客問我招驴,道長,這世上最難降的妖魔是什么枷畏? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任忽匈,我火速辦了婚禮,結(jié)果婚禮上矿辽,老公的妹妹穿的比我還像新娘丹允。我一直安慰自己,他們只是感情好袋倔,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布雕蔽。 她就那樣靜靜地躺著,像睡著了一般宾娜。 火紅的嫁衣襯著肌膚如雪批狐。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天前塔,我揣著相機與錄音嚣艇,去河邊找鬼。 笑死华弓,一個胖子當(dāng)著我的面吹牛食零,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播寂屏,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼贰谣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了迁霎?” 一聲冷哼從身側(cè)響起吱抚,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎考廉,沒想到半個月后秘豹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡昌粤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年既绕,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片婚苹。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡岸更,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出膊升,到底是詐尸還是另有隱情怎炊,我是刑警寧澤,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站评肆,受9級特大地震影響债查,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瓜挽,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一盹廷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧久橙,春花似錦俄占、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至祝拯,卻和暖如春甚带,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背佳头。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工鹰贵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人康嘉。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓碉输,卻偏偏與公主長得像,于是被迫代替她去往敵國和親凄鼻。 傳聞我的和親對象是個殘疾皇子腊瑟,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容