Android笔记 fragment的Tab样式demo

:android:minSdkVersion="11" 11才有的新特性

此种效果可以用TabHost实现 也可以用ViewPager实现(ViewPager没实现过 据说更美化)

demo

1布局

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tab1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="社会新闻" />

        <TextView
            android:id="@+id/tab2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="生活新闻" />

        <TextView
            android:id="@+id/tab3"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="军事新闻" />

        <TextView
            android:id="@+id/tab4"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="娱乐新闻" />
    </LinearLayout>

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

</LinearLayout>


fragment1.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:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是界面1" />

</LinearLayout>


fragment2.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:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是界面2" />

</LinearLayout>


fragment3.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:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是界面3" />

</LinearLayout>


fragment4.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:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是界面4" />

</LinearLayout>

2class

Fragment1.java

package com.example.a120fragmentchangeui;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment1, null);
	}
}


Fragment2.java

package com.example.a120fragmentchangeui;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment2, null);
	}
}


Fragment3.java

package com.example.a120fragmentchangeui;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment3 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment3, null);
	}
}


Fragment4.java

package com.example.a120fragmentchangeui;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment4 extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment4, null);
	}
}


MainActivity.java

package com.example.a120fragmentchangeui;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private LinearLayout content;
	private TextView tv1, tv2, tv3, tv4;
	private FragmentManager fm;
	private FragmentTransaction ft;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		content = (LinearLayout) findViewById(R.id.content);
		tv1 = (TextView) findViewById(R.id.tab1);
		tv2 = (TextView) findViewById(R.id.tab2);
		tv3 = (TextView) findViewById(R.id.tab3);
		tv4 = (TextView) findViewById(R.id.tab4);

		tv1.setOnClickListener(this);
		tv2.setOnClickListener(this);
		tv3.setOnClickListener(this);
		tv4.setOnClickListener(this);
		tv1.setBackgroundColor(Color.BLUE);
		fm = getFragmentManager();
		ft = fm.beginTransaction();

		ft.replace(R.id.content, new Fragment1());
		ft.commit();
	}

	@Override
	public void onClick(View v) {
		ft = fm.beginTransaction();
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tab1:
			tv1.setBackgroundColor(Color.BLUE);
			tv2.setBackgroundColor(Color.WHITE);
			tv3.setBackgroundColor(Color.WHITE);
			tv4.setBackgroundColor(Color.WHITE);
			ft.replace(R.id.content, new Fragment1());
			break;
		case R.id.tab2:
			tv1.setBackgroundColor(Color.WHITE);
			tv2.setBackgroundColor(Color.BLUE);
			tv3.setBackgroundColor(Color.WHITE);
			tv4.setBackgroundColor(Color.WHITE);
			ft.replace(R.id.content, new Fragment2());
			break;
		case R.id.tab3:
			tv1.setBackgroundColor(Color.WHITE);
			tv2.setBackgroundColor(Color.WHITE);
			tv3.setBackgroundColor(Color.BLUE);
			tv4.setBackgroundColor(Color.WHITE);
			ft.replace(R.id.content, new Fragment3());
			break;
		case R.id.tab4:
			tv1.setBackgroundColor(Color.WHITE);
			tv2.setBackgroundColor(Color.WHITE);
			tv3.setBackgroundColor(Color.WHITE);
			tv4.setBackgroundColor(Color.BLUE);
			ft.replace(R.id.content, new Fragment4());
			break;

		default:
			break;
		}
		ft.commit();
	}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值