設計元件的 weight, 讓UI符合各種大小不同裝置
如何以權重配置以下的 layout :
outline:
第一步驟 :
首先拉一個LinearLayout(Vertical)到版面並設定屬性 Height為 fill_parent
第二步驟 :
分別將 textView1、LinearLayout(horizontal)、LinearLayout(Vertical)、textView2 拉進第一步驟LinearLayout(Vertical)的底下
第三步驟 :
設定第二步驟的4個屬性, Height 皆為 fill_parent 並給它所要占據版面的Weight(權重)是多少
textView1
Height : fill_parent
Weight : 1.3
LinearLayout(horizontal)
Height : fill_parent
Weight : 1
LinearLayout(Vertical)
Height : fill_parent
Weight : 1
textView2
Height : fill_parent
Weight : 1.3
第四步驟 :
設定 textView1 屬性 Gravity 為 right
第五步驟 :
textView1
Height : fill_parent
Weight : 1.3
LinearLayout(horizontal)
Height : fill_parent
Weight : 1
LinearLayout(Vertical)
Height : fill_parent
Weight : 1
textView2
Height : fill_parent
Weight : 1.3
設定第2層 LinearLayout(horizontal) 的 Width為 fill_parent,並拉入3個button元件
第六步驟 :
設定第3層LinearLayout(Vertical) 的 Height 為 fill_parent,並拉入3個button元件
button1
Height: fill_parent
Weight : 1
button2
Height: fill_parent
Weight : 1
button3
Height: fill_parent
Weight : 1
結論 :
如果元件是水平配置,就將 Layout 和元件的 Width 都設為 fill_parent,在設計元件每個 Weight(權重)
若元件是垂直配置,則將 Layout 和元件的 Height 都設為 fill_parent,在設計元件的每個 Weight(權重)