一.FragmentTabHost的好处
在于可以更加灵活的设置按钮的样式.和轻易的与frgment切换进行绑定.节省了代码量
二.布局文件
主布局
<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="com.example.tabhost.MainActivity">
<FrameLayout
android:id="@+id/tabContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
<android.support.v4.app.FragmentTabHost
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v4.app.FragmentTabHost></LinearLayout>
tab标签的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/></LinearLayout>
三.代码
public class MainActivity extends AppCompatActivity {
@Bind(R.id.framlayout)
FrameLayout mFramlayout;
@Bind(R.id.btn1)
Button mBtn1;
@Bind(R.id.btn2)
Button mBtn2;
@Bind(R.id.btn3)
Button mBtn3;
@Bind(R.id.radio_group)
RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
iniView();
iniEvent();
}
private void iniEvent() {
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
BaseFragment fragment = null;
switch (checkedId) {
case R.id.btn1:
fragment = FragmentFractory.getInstance().getBaseFragment(0);
break;
case R.id.btn2:
fragment = FragmentFractory.getInstance().getBaseFragment(1);
break;
case R.id.btn3:
fragment = FragmentFractory.getInstance().getBaseFragment(2);
break;
}
transaction.replace(R.id.framlayout, fragment).commit();
}
});
}
private void iniView() {
getSupportFragmentManager().
beginTransaction().
replace(R.id.framlayout, FragmentFractory.getInstance().getBaseFragment(0))
.commit();
}
}
代码:
public class MainActivity extends AppCompatActivity {
private Class []clazz={Fragment1.class,Fragment2.class,Fragment3.class,Fragment4.class};
private FrameLayout mContainer;
private FragmentTabHost mTabHost;
String []tag={
"one",
"two",
"three",
"four"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContainer = (FrameLayout) findViewById(R.id.container);
mTabHost = (FragmentTabHost) findViewById(R.id.tab);
iniTab();
}
private void iniTab() {
mTabHost.setup(
this,getSupportFragmentManager(),R.id.container);
for (
int i =
0; i <tag.length ; i++) {
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(tag[i]);
View view =getView(i);
tabSpec.setIndicator(view);
mTabHost.addTab(tabSpec,clazz[i],
null);
}
}
private View
getView(
int i) {
TextView text=
new TextView(
this);
switch (i) {
case 0:
text.setText(
"首页");
break;
case 1:
text.setText(
"发现");
break;
case 2:
text.setText(
"购物");
break;
case 3:
text.setText(
"我的");
break;
}
return text;
}
}
布局文件
<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=
"com.example.administrator.recovertabfragmenthost.MainActivity">
<FrameLayout
android:id=
"@+id/container"
android:layout_width=
"match_parent"
android:layout_height=
"0dp"
android:layout_weight=
"1"></FrameLayout>
<android
.support.v4
.app.FragmentTabHost
android:id=
"@+id/tab"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"></android
.support.v4
.app.FragmentTabHost>
</LinearLayout>
源码下载 http://download.csdn.net/detail/dygcomed/9605219
转载请注明原文地址: https://ju.6miu.com/read-1310224.html