前往
大廳
主題

【Android筆記】Resource - 布局(Layout)

LF(小魚) | 2021-03-06 21:34:22 | 巴幣 0 | 人氣 142


一、介紹:
布局(Layout)就是一個可以決定要呈現在螢幕上的樣子,放在res/layout/底下的xml檔案
既然是XML檔,勢必會有規定的元素(Element)及屬性(Attribute):
元素:
<ViewGroup>
  或是其他可以放置View的任何元件,例如Layout類(LinearLayout,FrameLayout等)皆可
  屬性:
  android:id(Resource ID)
    唯一id,用來呼叫及指定View
  android:layout_height(Dimension or String)*
    高度,可以是維度或以下內容
描述
match_parent 設置成與父元素相同
wrap_conent 設置成適合的大小
  android:layout_width(Dimension or String)*
    寬度,接受值與android:layout_height相同

<View>
  或是其他單一View,例如TextView,Button等
  屬性:
  android:id(Resource ID)
  android:layout_height(Dimension or String)*
  android:layout_width(Dimension or String)*
    以上屬性皆與<ViewGroup>相同

<requestFocus>
  任何View或ViewGroup皆可放置此元素,代表其父元素聚焦時將焦點轉移至該元件上,某一元素底下之子元素只能有一個

<include>
  導入一個其他Layout內容
  屬性:
  layout(Layout Resource)*
    引用佈局資源
  android:id(Resource ID)
    覆蓋提供給佈局中的ID
  android:layout_height(Dimension or String)
    高度,當有android:layout_width時應用,接受值與<ViewGroup>的android:layout_height相同
  android:layout_width(Dimension or String)
    寬度,當有android:layout_height時應用,接受值與<ViewGroup>的android:layout_height相同

<merge>
  用來避免想要使用或重複利用某個Layout的內容,卻同時想要在導入時不使用某些特定元件,可以使用此元素指定那些元件在Layout被導入時應用

範例(來自Android官方文件):
<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/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button
android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>


創作回應

更多創作