您的位置:首页 > 移动开发 > Android开发

Android笔记 fragment的Tab样式demo

2014-10-22 19:17 134 查看
: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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: