標籤:android style blog http io ar color 使用 sp
1,使用<include /> 標籤避免代碼重複
設想一種情況:我們需要為應用程式中的每個視圖都添加一個標題。為了簡化問題,我們假設標題是一個顯示應用程式標題的TextView。通常多個Activity 會對應多個XML 檔案。難道我們需要把這個TextView 複製到每個XML 檔案中嗎?如果以後需要修改這個TextView 會出現什麼情況?“複製/ 粘貼”的方式固然能夠解決這個問題,但並不是高效的方法。解決上述問題的最簡單方法是使用<include /> 標籤。
我們可以通過<include /> 標籤把在其他XML檔案中定義的布局插入當前布局檔案中。以其中一個Activity 為例,其XML 布局檔案如下所示:
1 <RelativeLayout 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent"> 5 <TextView 6 android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:layout_centerInParent="true" 9 android:gravity="center_horizontal"10 android:text="@string/hello"/>11 <include layout="@layout/title"/>12 13 </RelativeLayout>
title布局如下所示:
1 <TextView xmlns:android="http://schemas.android.com/apk/res/android"2 android:layout_width="fill_parent"3 android:layout_height="wrap_content"4 android:layout_alignParentBottom="true"5 android:layout_marginBottom="30dp"6 android:gravity="center_horizontal"7 android:text="@string/title_text"/>
在上述範例程式碼中,我們使用了<include /> 標籤,並且只需要指定該標籤的layout 屬性值。
但是也存在一個問題,這種方式之所以可行是因為Activity 在main 布局檔案中使用的是RelativeLayout。如果其中一個Activity 的布局檔案使用的是LinearLayout 呢?
雖然android:layout_alignParentBottom="true" 適用於RelativeLayout,但是並不適用於LinearLayout。”這個想法是正確的。接下來分析
使用<include /> 標籤的第二種方法,在這種方法裡,我們直接在<include /> 標籤裡使用android:layout_* 屬性。
以下是修改後的main.xml 檔案,其中使用了<include /> 標籤的android:layout_* 屬性,源碼如下所示:
1 <RelativeLayout 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent"> 5 <TextView 6 android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:layout_centerInParent="true" 9 android:gravity="center_horizontal"10 android:text="@string/hello"/>11 <include12 layout="@layout/title"13 android:layout_width="fill_parent"14 android:layout_height="wrap_content"15 android:layout_alignParentBottom="true"16 android:layout_marginBottom="30dp"/>17 </RelativeLayout/>
修改後的標題布局檔案如下:
1 <TextView xmlns:android="http://schemas.android.com/apk/res/android"2 android:layout_width="0dp"3 android:layout_height="0dp"4 android:gravity="center"5 android:text="@string/title"/>
2,通過ViewStub 實現View 的消極式載入
設計布局的時候,讀者可能想過根據上下文或者使用者互動情況顯示一個視圖。如果想要一個視圖只在需要的時候顯示,可以使用ViewStub 這個類。
Android 開發文檔中有關於ViewStub 的介紹,主要內容如下:
“ViewStub 是一種不可視並且大小為0 的視圖,可以延遲到運行時填充(inflate) 布局資源。當ViewStub 設定為可視或者inflate() 方法被調用後,就會填充布局資源,然後ViewStub 便會被填充的視圖替代。”
既然已經清楚ViewStub 是什麼, 接下來看看它能做什麼。在下面的範例程式碼中,我們使用ViewStub 來消極式載入一個MapView。假設需要建立一個視圖來顯示地理位置的詳細資料,先看兩種可能情況:
? 一些場所沒有 GPS 資訊
? 使用者可能並不需要地圖資訊
如果一個場所沒有GPS 資訊,開發人員不需要在地圖上顯示標記資訊。同樣,如果使用者不需要地圖資訊,也就無須載入地圖。我們可以把MapView 放置在ViewStub 標籤中,讓使用者自己決定是否顯示地圖資訊。
要達到上述目的,需要使用下面的布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent"> 5 <Button 6 android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:text="@string/show_map" 9 android:onClick="onShowMap"/>10 <ViewStub11 android:id="@+id/map_stub"12 android:layout_width="fill_parent"13 android:layout_height="fill_parent"14 android:layout="@layout/map"15 andorid:inflatedId="@+id/map_view" />16 </RelativeLayout>
很顯然,需要通過map_stub這個id從Activity獲得ViewStub。同時layout屬性指定需要填充的布局檔案。對於本例,需要填充的是map.xml檔案,源碼如下:
1 <?xml version="1.0" encoding="utf-8"?>2 <com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"3 android:layout_width="fill_parent"4 android:layout_height="fill_parent"5 android:clickable="true"6 android:apiKey="my_api_key"/>
最後一個需要說明的屬性是inflatedId。inflatedId是調用ViewStub 的inflate() 方法或者setVisibility() 方法時返回的ID,這個ID 便是被填充的View 的ID。在本例中,我們不需要操作MapView,只需要調用setVisibility(View.VISIBLE) 方法即可。如果想擷取被填充的視圖的引用,inflate() 方法會直接返回該引用,這樣避免了再次調用findViewById() 方法。Activity 的源碼比較簡單,如下所示:
1 public class MainActivity extends MapActivity { 2 private View mViewStub; 3 @Override 4 public void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.main); 7 mViewStub = findViewById(R.id.map_stub); 8 } 9 public void onShowMap(View v) {10 mViewStub.setVisibility(View.VISIBLE);11 }12 ...13 }
如上述代碼所示,只需要改變ViewStub 的可視性便可控制map 的顯示。
還可以通過View view = ((ViewStub) findViewById(R.id.map_stub)).inflate();
當調用inflate()函數的時候,ViewStub被引用的資源替代,並且返回引用的view。 這樣程式可以直接得到引用的view而不用再次調用函數findViewById()來尋找了。
每日總結 - android布局最佳化