Android性能优化:ViewStub
用法一:
在開發應用程序的時候,經常會遇到這樣的情況,會在運行時動態根據條件來決定顯示哪個View或某個布局。那么最通常的想法就是把可能用到的View都寫在上面,先把它們的可見性都設為View.GONE,然后在代碼中動態的更改它的可見性。這樣的做法的優點是邏輯簡單而且控制起來比較靈活。但是它的缺點就是耗費資源。雖然把View的初始可見View.GONE但是在Inflate布局的時候View仍然會被Inflate,也就是說仍然會創建對象,會被實例化,會被設置屬性。也就是說,會耗費內存等資源。
推薦的做法是使用Android.view.ViewStub,ViewStub是一個輕量級的View,占用資源非常小的控件。在Inflate布局的時候,只有ViewStub會被初始化,然后當ViewStub被設置為可見的時候(默認是不可見的),或是調用了ViewStub.inflate()的時候,ViewStub所向的布局就才會被Inflate和實例化。
但ViewStub也不是萬能的,下面總結下ViewStub能做的事兒和什么時候該用ViewStub,什么時候該用可見性的控制。
首先來說說ViewStub的一些特點:
(1) ViewStub只能Inflate一次,之后ViewStub對象會被置為空。按句話說,某個被ViewStub指定的布局被Inflate后,就不會夠再通過ViewStub來控制它了。
(2) ViewStub只能用來Inflate一個布局文件,而不是某個具體的View,當然也可以把View寫在某個布局文件中。
基于以上的特點,那么可以考慮使用ViewStub的情況有:
(1) 在程序的運行期間,某個布局在Inflate后,就不會有變化,除非重新啟動。
因為ViewStub只能Inflate一次,之后會被置空,所以無法指望后面接著使用ViewStub來控制布局。所以當需要在運行時不止一次的顯示和隱藏某個布局,那么ViewStub是做不到的。這時就只能使用View來控制了。
(2) 想要控制顯示與隱藏的是一個布局文件,而非某個View。
因為設置給ViewStub的只能是某個布局文件的Id,所以無法讓它來控制某個View。
所以,如果想要控制某個View(如Button或TextView)的顯示與隱藏,或者想要在運行時不斷的顯示與隱藏某個布局或View,只能使用View來控制。
下面來看一個實例
在這個例子中,要顯示二種不同的布局,一個是用TextView顯示一段文字,另一個則是用ImageView顯示一個圖片。這二個是在onCreate()時決定是顯示哪一個,這里就是應用ViewStub的最佳地點。
先來看看布局,一個是主布局,里面只定義二個ViewStub,一個用來控制TextView一個用來控制ImageView,另外就是一個是為顯示文字的做的TextView布局,一個是為ImageView而做的布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="fill_parent" 6 android:layout_height="fill_parent" 7 android:gravity="center_horizontal"> 8 <ViewStub 9 android:id="@+id/viewstub_demo_text" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_marginLeft="5dip" 13 android:layout_marginRight="5dip" 14 android:layout_marginTop="10dip" 15 android:layout="@layout/viewstub_demo_text_layout"/> 16 <ViewStub 17 android:id="@+id/viewstub_demo_image" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_marginLeft="5dip" 21 android:layout_marginRight="5dip" 22 android:layout="@layout/viewstub_demo_image_layout"/> 23 </LinearLayout>
1 // ...
2 @Override
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.viewstub_demo_activity);
6 if ((((int) (Math.random() * 100)) & 0x01) == 0) {
7 ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text); // 獲取布局
8 stub.inflate(); // 實例化
9
10 TextView text = (TextView) findViewById(R.id.viewstub_demo_textview);
11 text.setText("Hello World");
12 } else {
13 ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_image);
14 stub.inflate();
15
16 ImageView image = (ImageView) findViewById(R.id.viewstub_demo_imageview);
17 image.setImageResource(R.drawable.img);
18 }
19 }
20 // ...
還有類似新內容、新功能提示,這種只會顯示一次,且不會發生變化的ViewStub都是挺適合的。
用法二:
ViewStub還有類似于<include>標簽的用法,只不過<ViewStub>是不可見的控件。當布局文件使用<ViewStub>標簽來引用其他布局文件時并不會馬上裝載引用的布局文件,只會在調用了ViewStub.inFlate獲ViewStub.setVisibility(View.VISIBLE)方法后,ViewStub才會裝載引用的控件。而<include>會把引用的所以布局文件一次性的全部加載,造成資源的浪費。下面看個實例:
custom.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 <Button 8 android:layout_width="match_parent" 9 android:layout_height="warp_parent" 10 android:text="我是按鈕1" 11 android:onclick="onClick_Button"> 12 <Button 13 android:layout_width="match_parent" 14 android:layout_height="warp_parent" 15 android:text="我是按鈕2" 16 android:onclick="onClick_Button"> 17 </LinearLayout>
采用<include>引用:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 <Button 8 android:layout_width="match_parent" 9 android:layout_height="warp_parent" 10 android:text="我是按鈕" 11 android:onclick="onClick_Button"> 12 <include layout="@layout/custom"/> 13 </LinearLayout>
圖一:采用<include>引用后顯示的效果
采用<ViewStub>引用:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 <Button 8 android:layout_width="match_parent" 9 android:layout_height="warp_parent" 10 android:text="我是按鈕" 11 android:onclick="onClick_Button"> 12 <ViewStub 13 android:id="@+id/viewstub" 14 android:inflatedId="@+id/button_layout" 15 layout="@layout/custom" 16 android:layout_width="match_parent" 17 android:layout_height="warp_parent"/> 18 </LinearLayout>
圖二:采用<ViewStub>引用后顯示的效果
在使用<ViewStub>標簽引用布局文件后,還需要調用ViewStub.inflate或ViewStub.setVisibility(View.VISIBLE)方法才能裝載所引用的空間,代碼如下:
1 public void onClick_Button(View v) {
2 // ViewStub 控件只能獲得1次,第2次再使用findViewById獲得該ViewStub對象,則返回null
3 View view = findViewById(R.id.viewstub);
4 if(view != null) {
5 // 或者調用ViewStub.inflate方法
6 // view = ((ViewStub) view).inflate();
7 // 裝載ViewStub引用的custom.xml文件的控件
8 ((ViewStub) view).setVisibility(View.VISIBLE);
9 } else {
10 setTitle("view is null");
11 }
12 }
單擊“我的按鈕”后,會顯示在custom.xml文件中定義的兩個按鈕,效果如圖二所示。
| 注意:<ViewStub>與<include>標簽一樣,也可以設置引用布局文件中根節點所有與布局相關的熟悉。所不同的是<include>標簽的 android:id 屬性直接覆蓋了所引用的布局文件中的根節點的 android:id 屬性值,而<ViewStub>標簽的 android:id 屬性與普通控件標簽的 android:id 屬性一樣,用于在代碼中引用控件,在<ViewStub>標簽中需要使用 android:inflatedId 屬性覆蓋所引用布局文件中根節點的 android:id 屬性值。 |
總結
以上是生活随笔為你收集整理的Android性能优化:ViewStub的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 飞行器设计专业就业(2022飞行器设计与
- 下一篇: 有关动物的四字词有哪些(有关动物的四字成