Android FragmentTabhost实现选项卡
2016-05-19 10:12
423 查看
在Android3.0之后,google创造了Fragment,因此原来的Tabhost已经不推荐使用了,现在一般推荐使用FragmentTabhost。
google考虑到了兼容问题,因此FragmentTabhost并未加在官方的SDK中,而是把它放在了android-support-v4.jar中
下面我带大家来实现一下这个功能。
1、MyFragmentTabhostActivity.java
2、Fragment.java
3、activity_fragment.xml
4、item_tab.xml
5、layout_fragment.xml
6、运行截图
google考虑到了兼容问题,因此FragmentTabhost并未加在官方的SDK中,而是把它放在了android-support-v4.jar中
下面我带大家来实现一下这个功能。
1、MyFragmentTabhostActivity.java
public class MyFragmentTabhostActivity extends FragmentActivity { private Context context; private FragmentTabHost fragmentTabHost = null; private Class[] fragmentArray = { MyFragment.class, MyFragment.class, MyFragment.class, MyFragment.class }; private int[] tabImageArray = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher }; private String[] tabTextArray = { "广场", "排名", "商城", "我" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragmenttabhost); context = this; initView(); } private void initView() { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); fragmentTabHost.setup(context, getSupportFragmentManager(), R.id.real_tabcontent); for (int i = 0; i < fragmentArray.length; i++) { // 给每个Tab按钮设置图标、文字和内容 TabSpec tabSpec = fragmentTabHost.newTabSpec(tabTextArray[i]).setIndicator(getTabItemView(inflater, i)); fragmentTabHost.addTab(tabSpec, fragmentArray[i], null); // 设置Tab按钮的背景 fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255))); } } public View getTabItemView(LayoutInflater inflater, int index) { View view = inflater.inflate(R.layout.item_tab, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageview); TextView textView = (TextView) view.findViewById(R.id.textview); imageView.setImageResource(tabImageArray[index]); textView.setText(tabTextArray[index]); return view; } }
2、Fragment.java
public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_fragment, null); view.setBackgroundColor(Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255))); return view; } }
3、activity_fragment.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" > <FrameLayout android:id="@+id/real_tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp"/> </android.support.v4.app.FragmentTabHost> </LinearLayout>
4、item_tab.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center_horizontal" android:orientation="vertical" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout> </RelativeLayout>
5、layout_fragment.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" > </LinearLayout>
6、运行截图
相关文章推荐
- Android开发之EditText属性详解
- android 中动态监听Edittext控件内容长度 TextWatcher监听事件
- Android sdk content loader 0%的解决方案
- Android插件化开发
- Android.mk中LOCAL_MODULE_TAGS的作用
- java/android/Process执行cmd命令行还可以进行封装
- android:wifi通信(一)
- Android.mk编译目标(编译包、二进制文件、预编译)
- Android——View的事件分发机制(学习Android开发艺术探索)
- android studio之jni(上)生成so文件
- Android studio 多渠道打包
- Android Studio右键选项中没有Git?
- Android 蓝牙,wlan调试命令
- 如何解决Android出现的异常“java.lang.NoSuchMethodError: android.widget.LinearLayout.setBackground”?
- android studio2.1创建项目的两个坑
- Android中的坐标
- Android WebView 与JS的数据交互
- 不需要任何权限获得Android设备的唯一ID,权限android设备id
- Android调试02
- 【Android】导入第三方库时,关于so文件的一些思考