2019-08-20 Android 線性布局介紹-LinearLayout

1.LinearLayout (線性布局)

?它是一個視圖組,所有的子元素都是單個方向的:

vertically(垂直) or horizontally(水平)套啤,要么都是垂直帝牡,要么都是水平婴洼。

屬性(attribute):android:orientation 海蔽。

屬性:android:layout_weight="1".

Google官方推薦,當使用weight屬性時输硝,將width設為0dip即可今瀑,效果跟設成wrap_content是一樣的。這樣weight就可以理解為占比了点把!

layout_width="wrap_content"只是在子LinearLayout布局下使用橘荠,并且有的也建議改成0dp.

(1)新建項目-androidLinearLayout

(2)修改layout的布局文件activity_main.xml:

? ?默認下的布局是RelativeLayout,(相對布局),

? ?然后就改成LinearLayout.

(3)編寫一個實例-用戶登錄界面(采用LinearLayout布局)

效果圖:

設計界面(可視化界面):

AVD上的效果圖:

?思路:從大到小的布局郎逃,均采用LinearLayout線性布局哥童。

? ? ? ? ? ? ? 再添加控件,進行里面的細節(jié)修改褒翰。

layout的布局文件activity_main.xml:

<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="com.example.andriod_linearlayout.MainActivity" >


? ? <LinearLayout

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content"? #根據內容設置

? ? android:background="#CCCCCC" #背景顏色

? ? android:orientation="horizontal">? #小布局-水平

? ? ? ? <TextView

? ? ? ? ? ? android:id="@+id/textView1"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="@string/username" /> #在values 的strings.xml下定義贮懈,消除下警告(黃色波浪線)

? ? ? ? <EditText

? ? ? ? ? ? android:id="@+id/editText1"

? ? ? ? ? ? android:layout_width="0dp"(消除黃色波浪線)

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="1"(“權重”)

? ? ? ? ? ? android:ems="10" >

? ? ? ? ? ? <requestFocus />

? ? ? ? </EditText>


</LinearLayout>


? ? <LinearLayout

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content"

? ? android:background="#CCCCCC"

? ? android:orientation="horizontal">

? ? ? ? <TextView

? ? ? ? ? ? android:id="@+id/textView2"

? ? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="@string/password" />

? ? ? ? <EditText

? ? ? ? ? ? android:id="@+id/editText2"

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_weight="0.76"

? ? ? ? ? ? android:ems="10"

? ? ? ? ? ? android:inputType="textPassword" />

? ? ? ? </LinearLayout>


? <LinearLayout

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content"

? ? android:background="#CCCCCC"

? ? android:gravity="center"

? ? android:orientation="horizontal">

? ? ? <Button

? ? ? ? ? android:id="@+id/button1"

? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? android:layout_weight="1"#權重?

? ? ? ? ? android:text="@string/login" />

? ? ? <Button

? ? ? ? ? android:id="@+id/button2"

? ? ? ? ? android:layout_width="wrap_content"

? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? android:layout_weight="1"

? ? ? ? ? android:text="@string/reset" />


? ? ? </LinearLayout>

</LinearLayout>


strings.xml 文件:

<?xml version="1.0" encoding="utf-8"?>

<resources>

? ? <string name="app_name">andriod_linearlayout</string>

? ? <string name="hello_world">Hello world!</string>

? ? <string name="action_settings">Settings</string>

? ? <string name="username">用戶名:</string>

? ? <string name="password">密碼:</string>

? ? <string name="login">登錄</string>

? ? <string name="reset">取消</string>

</resources>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市优训,隨后出現的幾起案子朵你,更是在濱河造成了極大的恐慌,老刑警劉巖揣非,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件抡医,死亡現場離奇詭異,居然都是意外死亡早敬,警方通過查閱死者的電腦和手機忌傻,發(fā)現死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來搞监,“玉大人芯勘,你說我怎么就攤上這事∠俟洌” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長棍矛。 經常有香客問我安疗,道長,這世上最難降的妖魔是什么够委? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任荐类,我火速辦了婚禮,結果婚禮上茁帽,老公的妹妹穿的比我還像新娘玉罐。我一直安慰自己,他們只是感情好潘拨,可當我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布吊输。 她就那樣靜靜地躺著,像睡著了一般铁追。 火紅的嫁衣襯著肌膚如雪季蚂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天琅束,我揣著相機與錄音扭屁,去河邊找鬼。 笑死涩禀,一個胖子當著我的面吹牛料滥,可吹牛的內容都是我干的。 我是一名探鬼主播艾船,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼葵腹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了丽声?” 一聲冷哼從身側響起礁蔗,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎雁社,沒想到半個月后浴井,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡霉撵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年磺浙,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片徒坡。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡撕氧,死狀恐怖,靈堂內的尸體忽然破棺而出喇完,到底是詐尸還是另有隱情伦泥,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站不脯,受9級特大地震影響府怯,放射性物質發(fā)生泄漏。R本人自食惡果不足惜防楷,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一牺丙、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧复局,春花似錦冲簿、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至龙优,卻和暖如春羊异,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背彤断。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工野舶, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人宰衙。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓平道,卻偏偏與公主長得像,于是被迫代替她去往敵國和親供炼。 傳聞我的和親對象是個殘疾皇子一屋,可洞房花燭夜當晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內容