Android 樣式布局

來源:互聯網
上載者:User

標籤:內容   添加   布局檔案   工作   檔案中   back   roi   ble   android   

一、樣式布局

  首先,先看下面這段樣式布局代碼,這裡稱在xml控制項上添加屬性為內聯(僅限於本篇博文這樣稱呼):

<Button    android:id="@+id/crime_date"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="16dp"    android:layout_marginRight="16dp"/><CheckBox    android:id="@+id/crime_solved"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="16dp"    android:layout_marginRight="16dp"    android:text="@string/solved_btn"/><Button    android:id="@+id/choose_btn"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="16dp"    android:layout_marginRight="16dp"    android:text="@string/choose_name"/><Button    android:id="@+id/contact_btn"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="16dp"    android:layout_marginRight="16dp"    android:text="@string/contacter_name"/>

  在上面這段XML布局代碼,可以看出,各個按鈕的樣式完全一樣,添加某個屬性給控制項就有多次。假如,有更多的控制項使用相同的而已,要做N次重複的工作。Android提供了各種樣式UI,可用於解決重複性的工作。樣式資源類似於CSS樣式。樣式也可以具有層級結構:子樣式擁有父式樣同樣的屬性及屬性值,可以覆蓋它們,也可以添加新的屬性。

  類似字元資源,樣式定義在XML檔案的<Resources>酬標籤內,並存放在res/values目錄中。另外,資源檔取什麼名並不重要,但根據約定,樣式通常定義在style.xml檔案中。在Android項目中,已經預設建立了這個檔案。

  Style.xml檔案:

1 <resources>2     <style name="Crime_btn_style1">3         <item name="android:layout_width">wrap_content</item>4         <item name="android:layout_height">wrap_content</item>5         <item name="android:layout_marginLeft">16dp</item>6         <item name="android:layout_marginRight">16dp</item>7     </style>8 </resources>

  layout/fragment_crime.xml檔案,將控制項的屬性以style樣式名引入的方式在外聯(僅限於本篇博文這樣稱呼):

 1 <Button 2     android:id="@+id/crime_date" 3     style="@style/Crime_btn_style1"/> 4  5 <CheckBox 6     android:id="@+id/crime_solved" 7     android:text="@string/solved_btn" 8     style="@style/Crime_btn_style1"/> 9 10 <Button11     android:id="@+id/choose_btn"12     android:text="@string/choose_name"13     style="@style/Crime_btn_style1"/>14 15 <Button16     android:id="@+id/contact_btn"17     android:text="@string/contacter_name"18     style="@style/Crime_btn_style1"/>

 PS:需要注意的是,在內聯屬性與外聯屬性衝突時,以內聯屬性為準,也可以說,內聯屬性優於外聯屬性方式。

二、include與merge

  include使用資源ID引入而已檔案。

  layout/button_row.xml

1 <TableRow2 xmlns:android="http:/schemas.android.com/apk/res/android">3     <Button style="@style/Crime_btn_style1">4     <Button style="@style/Crime_btn_style1">5     <Button style="@style/Crime_btn_style1">6 </TableRow>

  layout/fragment_crime.xml

1 <include2     android:layout_weight="1"3     layout="@layout/button_row" />4 <include5     android:layout_weight="1"6     layout="@layout/button_row" />7 <include8     android:layout_weight="1"9     layout="@layout/button_row" />

以上代碼錶明,include將引入資源ID為button_row檔案內容。

PS:include標籤只能引用layout布局檔案,也就是只能引用layout目錄下的布局檔案。

Android 樣式布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.