标签:otf cte apk xmlns icon tabs setup prot inittab
在开发商城上中时为了实现底部导航栏使用了fragmentTabHost方法,总结如下:
1、Activity必须继承fragmentActivity方法。
2.必须调用setup()方法
3.添加TabSpec
具体代码如下:
主要布局:
activity_main:
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@color/colorAccent"
/>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
主要代码逻辑:
public class MainActivity extends AppCompatActivity {
private FragmentTabHost mTabHost;
private LayoutInflater mInflater;
private List<Tab> mTabs = new ArrayList<>(5);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTab();
}
private void initTab() {
Tab tab_home = new Tab(HomeFragment.class,R.string.home,R.mipmap.icon_home);
Tab tab_hot = new Tab(HotFragment.class,R.string.hot,R.mipmap.icon_hot);
Tab tab_category = new Tab(CategoryFragment.class,R.string.catagory,R.mipmap.icon_discover);
Tab tab_cart = new Tab(CartFragment.class,R.string.cart,R.mipmap.icon_cart);
Tab tab_mine= new Tab(MineFragment.class,R.string.mine,R.mipmap.icon_user);
mTabs.add(tab_home);
mTabs.add(tab_hot);
mTabs.add(tab_category);
mTabs.add(tab_cart);
mTabs.add(tab_mine);
mInflater = LayoutInflater.from(this);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
for(Tab tab : mTabs){
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(tab.getTitle()));
tabSpec.setIndicator( buildindictor(tab));
mTabHost.addTab(tabSpec,tab.getFragment(),null);
}
}
private View buildindictor(Tab tab){
View view = mInflater.inflate(R.layout.tab_indication,null);
ImageView img = (ImageView) findViewById(R.id.icon_tab);
TextView txt = (TextView) findViewById(R.id.txt_indicator);
img.setBackgroundResource(tab.getIcon());
txt.setText(tab.getTitle());
return view;
}
}
在新建CartFragment:\CategoryFragment;\.HomeFragment;\.HotFragment;\.MineFragmen类,都必须继承v4包下的Fragmentpublic class HotFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_hot,container,false);
}
}
标签:otf cte apk xmlns icon tabs setup prot inittab
原文地址:http://www.cnblogs.com/qq1107625225/p/7029402.html