android 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的嵌套



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
viewSwitcher一个布局容器,可以用于实现切换不同的布局视图。它能够在两个或多个视图之间切换,并且可以设置切换的动画效果。 下面是一个简单的例子,演示如何使用viewSwitcher实现切换两个不同的布局视图: 1. 定义两个布局文件: 布局1:activity_main.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFC0CB"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is layout 1" android:textSize="30sp" android:layout_centerInParent="true"/> </RelativeLayout> ``` 布局2:activity_second.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout2" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#008B8B"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is layout 2" android:textSize="30sp" android:layout_centerInParent="true"/> </RelativeLayout> ``` 2. 在activity_main.xml中添加viewSwitcher布局容器: ``` <?xml version="1.0" encoding="utf-8"?> <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewSwitcher" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/activity_main"/> <include layout="@layout/activity_second"/> </ViewSwitcher> ``` 3. 在Activity中实现切换功能: ``` public class MainActivity extends AppCompatActivity { private ViewSwitcher viewSwitcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher); // 设置切换动画效果 viewSwitcher.setInAnimation(this, android.R.anim.fade_in); viewSwitcher.setOutAnimation(this, android.R.anim.fade_out); // 点击布局1切换到布局2 findViewById(R.id.layout1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewSwitcher.showNext(); } }); // 点击布局2切换到布局1 findViewById(R.id.layout2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewSwitcher.showPrevious(); } }); } } ``` 在Activity中获取到viewSwitcher实例,并设置切换动画效果。然后通过监听布局1和布局2的点击事件来实现切换功能。当点击布局1时,调用viewSwitcher.showNext()方法切换到布局2;当点击布局2时,调用viewSwitcher.showPrevious()方法切换到布局1。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值