第一行代碼Android第三課

要點(diǎn):

android:orientation="vertical"垂直線性布局,"horizontal"水平線性布局

android:gravity="top"(buttom、left冈涧、right、center_vertical正蛙、fill_vertical督弓、center_horizontal、fill_horizontal乒验、center愚隧、fill、clip_vertical徊件、clip_horizontal)控制布局中控件的對齊方式奸攻。如果是沒有子控件的控件設(shè)置此屬性,表示其內(nèi)容的對齊方式虱痕,比如說TextView里面文字的對齊方式睹耐;若是有子控件的控件設(shè)置此屬性,則表示其子控件的對齊方式部翘,gravity如果需要設(shè)置多個(gè)屬性值硝训,需要使用“|”進(jìn)行組合

android:gravity 與 android:layout_gravity的區(qū)別

android:gravity是指定本元素的子元素相對它的對齊方式。

android:layout_gravity是指定本元素相對它的父元素的對齊方式新思。

android:layout_weight="1"通過設(shè)置控件的layout_weight屬性以控制各個(gè)控件在布局中的相對大小,線性布局會根據(jù)該控件layout_weight值與其所處布局中所有控件layout_weight值之和的比值為該控件分配占用的區(qū)域窖梁。在水平布局的LinearLayout中有兩個(gè)Button,這兩個(gè)Button的layout_weight屬性值都為1夹囚,那么這兩個(gè)按鈕都會被拉伸到整個(gè)屏幕寬度的一半纵刘。如果layout_weight指為0,控件會按原大小顯示荸哟,不會被拉伸假哎;對于其余l(xiāng)ayout_weight屬性值大于0的控件瞬捕,系統(tǒng)將會減去layout_weight屬性值為0的控件的寬度或者高度,再用剩余的寬度或高度按相應(yīng)的比例來分配每一個(gè)控件顯示的寬度或高度舵抹。

圖片來自網(wǎng)絡(luò)



#源代碼

'''<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:orientation="vertical"
??? tools:context=".LinearLayoutActivity" >
?<LinearLayout
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"
??????? android:layout_weight="1"
??????? android:orientation="horizontal" >

<Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#aa0000"
??????????? android:gravity="center_horizontal|center_vertical"
??????????? android:text="第一列"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#00aa00"
??????????? android:gravity="center_horizontal"
??????????? android:text="第二列"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#0000aa"
??????????? android:gravity="center|bottom"
??????????? android:text="第三列"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#aaaa00"
??????????? android:gravity="bottom"
??????????? android:text="第四列"
??????????? android:textSize="15sp" >
??????? </Button>
??? </LinearLayout>

??? <LinearLayout
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"
??????? android:layout_weight="1"
??????? android:orientation="vertical" >

??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第1行"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第2行"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第3行"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第4行"
??????????? android:textSize="15sp" >
??????? </Button>
??? </LinearLayout>

</LinearLayout>'''

'''<Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#aa0000"
??????????? android:gravity="center_horizontal|center_vertical"
??????????? android:text="第一列"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#00aa00"
??????????? android:gravity="center_horizontal"
??????????? android:text="第二列"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#0000aa"
??????????? android:gravity="center|bottom"
??????????? android:text="第三列"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#aaaa00"
??????????? android:gravity="bottom"
??????????? android:text="第四列"
??????????? android:textSize="15sp" >
??????? </Button>
??? </LinearLayout>

??? <LinearLayout
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"
??????? android:layout_weight="1"
??????? android:orientation="vertical" >

??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第1行"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第2行"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第3行"
??????????? android:textSize="15sp" >
??????? </Button>

??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第4行"
??????????? android:textSize="15sp" >
??????? </Button>
??? </LinearLayout>

</LinearLayout>'''

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肪虎,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子惧蛹,更是在濱河造成了極大的恐慌扇救,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件香嗓,死亡現(xiàn)場離奇詭異迅腔,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)陶缺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進(jìn)店門钾挟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人饱岸,你說我怎么就攤上這事掺出。” “怎么了苫费?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵汤锨,是天一觀的道長。 經(jīng)常有香客問我百框,道長闲礼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任铐维,我火速辦了婚禮柬泽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘嫁蛇。我一直安慰自己锨并,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布睬棚。 她就那樣靜靜地躺著第煮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪抑党。 梳的紋絲不亂的頭發(fā)上包警,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天,我揣著相機(jī)與錄音底靠,去河邊找鬼害晦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛暑中,可吹牛的內(nèi)容都是我干的篱瞎。 我是一名探鬼主播苟呐,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼痒芝,長吁一口氣:“原來是場噩夢啊……” “哼俐筋!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起严衬,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤澄者,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后请琳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體粱挡,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年俄精,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了询筏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,615評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡竖慧,死狀恐怖嫌套,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情圾旨,我是刑警寧澤踱讨,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站砍的,受9級特大地震影響痹筛,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜廓鞠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一帚稠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧床佳,春花似錦滋早、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至怨绣,卻和暖如春角溃,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背篮撑。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工减细, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人赢笨。 一個(gè)月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓未蝌,卻偏偏與公主長得像驮吱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子萧吠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評論 2 359

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

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程左冬,因...
    小菜c閱讀 6,444評論 0 17
  • 歡迎Follow我的GitHub, 關(guān)注我的CSDN. 其余參考Android目錄. 轉(zhuǎn)載請注明出處:http:/...
    passiontim閱讀 4,772評論 0 31
  • Android 4.0的SDK已經(jīng)發(fā)布,在眾多的新增特性中纸型,其中對開發(fā)者來說比較重要的特性之一拇砰,是新增的兩種界面布...
    扈博鑫閱讀 1,209評論 0 6
  • Android功能強(qiáng)大,界面華麗狰腌,但是眾多的布局屬性就害苦了開發(fā)者除破,下面這篇文章結(jié)合了網(wǎng)上不少資料.第一類:屬性值...
    HangChen閱讀 4,883評論 0 24
  • 多少事事心中默,今宵更與何人說琼腔? 萍水相逢是兄弟瑰枫,情長意久成陌客。 唯有酸澀杯中酒丹莲,此夜隨風(fēng)與吾歌光坝。 不在其中不落...
    不知世間何事閱讀 439評論 0 2