android FragmentTabHost的简单使用
2014-12-11 13:10
537 查看
在一篇博客里看到一个demo,多多少少有点问题,就拿来自己修改了下,做备份。
东西比较简单,一个Acitvity,一个layout。下面上代码。
1. FragmentTabHostActivity
2.layout_fragment_tab_host.xml
东西比较简单,一个Acitvity,一个layout。下面上代码。
1. FragmentTabHostActivity
package com.test.other; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.test.myapplication.R; import java.util.List; public class FragmentTabHostActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_fragment_tab_host); final FragmentTabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); mTabHost.addTab(mTabHost.newTabSpec("0").setIndicator("新闻"), NewsFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator("音乐"), LifeFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("2").setIndicator("人生"), NewsFragment.class, null); ((Button)findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { List<Fragment> fragments=getSupportFragmentManager ().getFragments(); Fragment fragment=fragments.get(mTabHost.getCurrentTab()); String str=((TextView)fragment.getView().findViewById(android.R.id.button1)).getText().toString(); Toast.makeText(FragmentTabHostActivity.this,str,Toast.LENGTH_LONG).show(); } }); } public static class NewsFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setId(android.R.id.button1); textView.setText("NewsFragment"); return textView; } } public static class LifeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setId(android.R.id.button1); textView.setText("LifeFragment"); return textView; } } }
2.layout_fragment_tab_host.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"> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#abc123" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#555aec" android:orientation="horizontal" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffaa476d" /> </LinearLayout> </android.support.v4.app.FragmentTabHost> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="click" /> </LinearLayout>
相关文章推荐
- android FragmentTabHost 的简单使用
- Android之---FragmentTabHost使用简单介绍
- Android-FragmentTabHost的简单使用
- android FragmentTabHost 的简单使用
- Android仿新浪微博底栏(FragmentTabHost的使用)
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- Android 关于ViewPager结合碎片Fragment的简单使用
- Android Fragment应用实战,使用碎片向TabHost和ActivityGroup说再见
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- Android Fragment简单使用
- android 音乐播放器制作 之 Tabhost 简单使用(一)
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- [android]使用TabHost+Fragment(替… 分类: Android开发 2014-05-30 10:57 108人阅读 评论(0) 收藏
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- [android]使用TabHost+Fragment(替…
- android 中Fragment 的简单使用
- Android常用控件之FragmentTabHost的使用
- 39 Android fragment 简单使用
- android DialogFragment简单使用
- Android使用Fragment应用实战,简单的实习例子