FragmentTabHost的应用既简单又好用还是不说了看main_activity布局;
<span style="font-size:18px;"> <span style="font-size:18px;"> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="60dp">
</android.support.v4.app.FragmentTabHost></span></span>
下面是Activity的代码,只是 最重要的;
<span style="font-size:18px;">package com.example.fragmenttabhost;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import static android.R.attr.fragment;
import static android.R.attr.tag;
public class MainActivity extends FragmentActivity {
private FragmentTabHost tabhost;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
//初始化tabhost
tabhost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// 设置tabhost的 参数。第一个参数是上下文,第二个是碎片管理者,第三个是要操作碎片的位置。
tabhost.setup(this,getSupportFragmentManager(),android.R.id.tabcontent);
// 设置取消按钮的分割线
tabhost.getTabWidget().setDividerDrawable(null);
//
TabHost.TabSpec a = tabhost.newTabSpec("a");
//设置指示器就是按钮的文本
a.setIndicator("主页");
//指示器可以与bundle绑定进行传值
Bundle bundleA= new Bundle();
bundleA.putString("name","主页");
//第一个参数 是a,第二个是碎片,第三个是bundle的 对象
tabhost.addTab(a,Afragment.class,null);
TabHost.TabSpec b = tabhost.newTabSpec("b");
b.setIndicator("我的");
Bundle bundleB= new Bundle();
bundleB.putString("ame","你好");
tabhost.addTab(b,Bfragment.class,null);
TabHost.TabSpec c= tabhost.newTabSpec("c");
c.setIndicator("附近");
Bundle bundleC= new Bundle();
bundleC.putString("name","我是c");
tabhost.addTab(c,Cfragment.class,null);
TabHost.TabSpec d= tabhost.newTabSpec("d");
d.setIndicator("好好");
Bundle bundleD= new Bundle();
bundleD.putString("name","我是c");
tabhost.addTab(d,Dfragment.class,null);
}
}</span>
<span style="font-size:18px;">
</span>
以下是基类;
<span style="font-size:18px;">package com.example.fragmenttabhost;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by 孙贝贝 on 2016/9/27.
*/
public abstract class BaseFragment extends Fragment {
Context context;
public View view;
public TextView content;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setArgs();
return initView();
}
public abstract void setArgs();
public View initView(){
view = LayoutInflater.from(context).inflate(R.layout.fragment_content,null);
content = (TextView) view.findViewById(R.id.content);
initContent();
return view;
}
public abstract void initContent();
}
</span>
基类中加载的布局:
<?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">
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
下面是碎片样式:
<span style="font-size:18px;">package com.example.fragmenttabhost;
import android.os.Bundle;
import android.support.annotation.Nullable;
/**
* Created by 孙贝贝 on 2016/9/27.
*/
public class Afragment extends BaseFragment{
private String name;
@Override
public void setArgs() {
context = getActivity();
}
@Override
public void initContent() {
content.setText("");
}
//
// @Override
// public void onCreate(@Nullable Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// Bundle bundle = getArguments();
// name = (String) bundle.get("name");
// }
}
</span>
完成哈哈