最近手头上有一个项目,因为是刚入行不久的菜鸟. 所以学习了关于高级控件的东西
给大家贴出来的就是像tabhost一样的选项卡
main.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" style="@android:style/Theme.Black.NoTitleBar.Fullscreen" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:onClick="onClick" > <TextView android:id="@+id/tab1" android:text="军事" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" /> <TextView android:id="@+id/tab2" android:layout_width="0dp" android:layout_weight="1" android:text="生活" android:layout_height="wrap_content" android:gravity="center" /> <TextView android:text="娱乐" android:id="@+id/tab3" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" /> <TextView android:text="政治" android:id="@+id/tab4" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/ment" android:orientation="vertical" > </LinearLayout> </LinearLayout>
然后大家自己再建立四个布局文件 命名v1,v2,v3等.可以自己在每个选项卡布局文件中放置不同的图片或者文字
这个是选项卡布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" style="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:orientation="vertical" > <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="200dp" android:background="@drawable/pic1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/zza" android:text="loading.." /> </LinearLayout>
下面贴出代码:
MainActivity.java
package com.bkw.tzof; import android.annotation.SuppressLint; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; import android.widget.TextView; @SuppressLint("NewApi") public class FragMentActivty extends Activity implements OnClickListener{ private TextView tv1,tv2,tv3,tv4; LinearLayout ment; FragmentManager fm; //fragement管理器 FragmentTransaction ft; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); ment=(LinearLayout) findViewById(R.id.ment); 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); fm=getFragmentManager(); ft=fm.beginTransaction(); ft.replace(R.id.ment, new Fragment1()); ft.commit(); } public void onClick(View v){ ft=fm.beginTransaction(); switch (v.getId()) { case R.id.tab1: ft.replace(R.id.ment, new Fragment1()); break; case R.id.tab2: ft.replace(R.id.ment, new Fragment2()); break; case R.id.tab3: ft.replace(R.id.ment, new Fragment3()); break; case R.id.tab4: ft.replace(R.id.ment, new Fragment4()); break; default: break; } ft.commit(); } }
Fragment1.java (其它几个渲染选项卡也是这样子写)
/** * */ package com.bkw.tzof; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * @author baikw * com.bkw.tzof * * 2014-1-6 * 下午1:44:11 */ @SuppressLint("NewApi") public class Fragment2 extends Fragment { /* (non-Javadoc) * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.frag2, null); } }
运行效果就ok了