Android应用的tab切换效果

在进行Android开发时经常配到,要使用Tab的情况,遇到不同的项目老大时,没人都还有自己的风格。下面续写一下我总结的自己觉得要注意的几种方式。

第一:使用Tabhost+Fragment实现tab切换,并通过Fragment转载xml并绑定监听事件可以实现响应xml的效果。

首先mian.xml文件

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <FrameLayout
                android:id="@+id/fragment_phone"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </FrameLayout>

            <FrameLayout
                android:id="@+id/fragment_mess"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </FrameLayout>
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:tag="tabs" >
        </TabWidget>
    </LinearLayout>

</TabHost>


下面通过Fragment转载xml来表示要切换的页面  这里xml中仅仅放一个图片
mess.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/xianjian01"
        />
</LinearLayout>

phone.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/xianjian02"
        />
</LinearLayout>


 
public class MessFragment extends Fragment {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.mess, container,false);
	}
	
}
public class PhoneFragment extends Fragment {

	
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.phone, container,false);
	}
	
}

public class MainActivity extends android.support.v4.app.FragmentActivity  implements OnTabChangeListener{

	private String [] titles= {"电话","信息"};
	private int [] tabIDs = {R.id.fragment_phone,R.id.fragment_mess};
	private TabHost mhost;
	private Fragment frag=null;
	private int convertID;
	private FragmentManager manager = getFragmentManager();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView(){
		mhost = (TabHost)findViewById(android.R.id.tabhost);
		mhost.setup();
		for (int i = 0; i < tabIDs.length; i++) {
			Button button = new Button(this);
			
			button.setText(titles[i]);
			mhost.addTab(mhost.newTabSpec(titles[i]).setIndicator(button).setContent(tabIDs[i]));
		}
		mhost.setOnTabChangedListener(this);
		frag = new PhoneFragment(); 
		 convertID = R.id.fragment_phone;
		 FragmentTransaction trans = manager.beginTransaction();
         trans.replace(convertID, frag, titles[0]);
         trans.commit();
	}
	

	@SuppressLint("NewApi")
	@Override
	public void onTabChanged(String tag) {
		// TODO Auto-generated method stub
		if(tag.equals(titles[0])){
			
			 frag = new PhoneFragment(); 
			 convertID = R.id.fragment_phone;
		}else if (tag.equals(titles[1])) {
			frag = new MessFragment();
			convertID = R.id.fragment_mess;
		}
		if(tag==null){
			return;
		}
		
		
		if (manager.findFragmentByTag(tag) == null) {
            FragmentTransaction trans = manager.beginTransaction();
            trans.replace(convertID, frag, tag);
            trans.commit();
        }
	}

}


当然你如果要实现复杂的界面,只需要在onCreateView 中得到xml 中的控件,,并为其制定事件监听即可
 
这样就可以实现切换了效果如下:

如果要实现下面Tab的边效果 只要制定一个xml配置文件然后制定那个图片选中或者那种背景颜色即可;第二种机会讲到
第二;实现微博那样的菜单效果:已经有人做了很好的总结,就不写了
 http://blog.csdn.net/yangyu20121224/article/details/9016223
 
 
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值