android ViewSwitcher實現視圖的輕鬆切換,viewswitcher

來源:互聯網
上載者:User

android ViewSwitcher實現視圖的輕鬆切換,viewswitcher

以前在使用listview或者gridview這種東西的時候,如果想要添加一個沒有內容時的友好提醒,是在該listview或者gridview的相同位置上,添加一個imageview,然後通過對該控制項的visibile進行設定成View.visible或者View.gone方法設定,這種雖然也能實現,但是無疑是比較繁瑣的,現在我們有了更好的實現方法,那就是使用ViewSwitcher

範例程式碼如下:

<ViewSwitcher                    android:id="@+id/vs_dynamic"                    android:layout_width="match_parent"                    android:layout_height="155dp"                    android:layout_margin="10dp" >                    <ListView                        android:id="@+id/lv_dynamic"                        style="@style/listviewbackground"                        android:layout_width="match_parent"                        android:layout_height="155dp"                        android:layout_margin="10dp" >                    </ListView>                    <ImageView                        android:id="@+id/iv_dynamic_nocontent"                        style="@style/listviewbackground"                        android:layout_width="match_parent"                        android:layout_height="155dp"                        android:layout_margin="10dp"                        android:src="@drawable/viewpager3"                        android:visibility="visible" >                    </ImageView>                                    </ViewSwitcher>
在我們的程式中通過id找到我們的ViewSwitcher

<pre name="code" class="java"><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">ViewSwitcher </span><span style="font-family: Arial, Helvetica, sans-serif;">dynamicViewSwitcher = (ViewSwitcher) findViewById(R.id.vs_dynamic);</span>


然後通過

dynamicViewSwitcher.setDisplayedChild(1);
來設定我們將要顯示的是哪一個child視圖,在該控制項的使用中,需要注意注意的地方是ViewSwitcher只能有兩個子視圖,不然的話就會異常,如果想要添加比較複雜的view

我們可以考慮使用layout以及使用layout的嵌套



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.