采取的方法是Fragment+FragmentTabHost组件来实现这种常见的app主页面的效果
首先给出main.xml文件
<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <FrameLayout androID:ID="@+ID/realtabcontent" androID:layout_wIDth="fill_parent" androID:layout_height="0dip" androID:layout_weight="1" androID:background="@color/white" /> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="bottom" androID:orIEntation="vertical"> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="1px" androID:background="@color/color_home_tab_line" /> <androID.support.v4.app.FragmentTabHost androID:ID="@androID:ID/tabhost" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:background="@color/et_divIDer_disable"> <FrameLayout androID:ID="@androID:ID/tabcontent" androID:layout_wIDth="0dp" androID:layout_height="0dp" androID:layout_weight="0" /> </androID.support.v4.app.FragmentTabHost> </linearLayout> </linearLayout>
主代码:
public class MainActivity { @VIEwInject(androID.R.ID.tabhost) private FragmentTabHost mTabHost; private LayoutInflater layoutInflater; private int mImageVIEwArray[] = {R.drawable.home_tab1,R.drawable.home_tab2,R.drawable.home_tab3,R.drawable.home_tab4}; private String mTextvIEwArray[] = {"首页","圈子","资讯","个人中心"}; private Class fragmentArray[] = {Fragment1.class,Fragment2.class,Fragment3.class,Fragment4.class}; protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } @OverrIDe protected voID init() { // List=new JsONArray(); layoutInflater=LayoutInflater.from(this); initTabHost();//初始化底部菜单 } /** * 初始化底部工具栏 */ private voID initTabHost() { mTabHost = (FragmentTabHost) findVIEwByID(androID.R.ID.tabhost); mTabHost.setup(this,getSupportFragmentManager(),R.ID.realtabcontent); int count = fragmentArray.length; for (int i = 0; i < count; i++) { TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextvIEwArray[i]) .setIndicator(getTabItemVIEw(i)); mTabHost.addTab(tabSpec,fragmentArray[i],null); mTabHost.getTabWidget().getChildAt(i) .setBackgroundResource(R.color.white); } mTabHost.setCurrentTabByTag(mTextvIEwArray[0]); mTabHost.getTabWidget().setdivIDerDrawable(null); } /** * 项的样式 * @param index 第几个 * @return 每一个Tab样式 */ private VIEw getTabItemVIEw(int index) { VIEw vIEw = layoutInflater.inflate(R.layout.tab_home_item,null); ImageVIEw imageVIEw = (ImageVIEw) vIEw.findVIEwByID(R.ID.icon); imageVIEw.setimageResource(mImageVIEwArray[index]); TextVIEw textVIEw = (TextVIEw) vIEw.findVIEwByID(R.ID.name); textVIEw.setText(mTextvIEwArray[index]); return vIEw; } }
通过以上文章,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)全部内容,希望文章能够帮你解决Android Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)