iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 8
0

版面配置參數
命名為layout_something 的 XML 版面配置屬性適用於所屬 ViewGroup 的 View 定義版面配置參數。
所有 ViewGroup 類別都會實作可延伸 ViewGroup.LayoutParams 的巢狀類別。這個子類別包含定義下層檢視大小和位置的屬性類型,而這些屬性類型也適用於檢視群組。 如圖,上層檢視群組為每個下層檢視 (包括下層檢視群組) 定義了版面配置參數。

https://ithelp.ithome.com.tw/upload/images/20200921/20130598f1YYr4lYvW.png

上圖為檢視層級以及與每個檢視相關聯的版面配置參數

每個版面配置由必須為View或ViewGroup 組成,如直向 LinearLayout 以放 TextView 和 Button 的 XML 版面配置:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/inputtext"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="輸入資料" />
    <Button android:id="@+id/addbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="新增" />
</LinearLayout>

屬性(Attributes)
每個 View 和 ViewGroup 物件均支援本身專用的各種 XML 屬性,也有 View 基礎屬性如id 屬性,這些屬性作為「版面配置參數」,這些屬性可說明 View 物件的特定版面配置方向 (View 由物件的 ViewGroup 上層物件所定義)。

ID

開頭的 @ 符號可指示 XML 剖析或展開 ID 字串的其餘部分。 (+) 表示此為系統必須建立並加到資源 (R.java 檔案) 中的新資源名稱 ,如下為button 所示,從版面配置中取得物件以ID方式,故不重複ID:

建立版面配置

<Button android:id="@+id/test_button"
 android:layout_width="wrap_content"
            android:layout_height="wrap_content/>

取得版面配置中取得物件

Button myButton = (Button) findViewById(R.id.test_button);

*補充: layoutwidth 和 layoutheight *

常用設定寬度或高度:

  • wrap_content 自行將大小調整成其內容所需的尺寸。
  • match_parent 自行將大小擴展成上層檢視群組允許的上限。

reference :https://developer.android.com/guide/topics/ui/declaring-layout
reference :https://www.runoob.com/w3cnote/android-tutorial-view-viewgroup-intro.html


上一篇
<DAY7> 支援不同裝置解析度
下一篇
<Day9>ConstraintLayout
系列文
Android App 初學者從零開始用Kotlin寫APP30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言