首页 > 技术文章 > Android TabHost控件 右侧留空并增加按钮

mnight 2013-10-14 20:52 原文

涉及公司内部程序,部分地方进行模糊处理。



公司Android程序的一个子程序UI要进行改版,最初的UI添加按钮是在内容区,而且TabHost空间是正常的标题平均分布。如下图(其实这是改版的第一版,没有了添加按钮,但是还是标题还是平均分布。)




但是,后来改版后,“添加”按钮变成了这样:



这个界面上,首先,TabHost集中在左侧,然后“加号”按钮变到了右侧,而且第一和第二个Tab有按钮,第三个“设置”Tab没有加号。


由于是在原基础上改版,源程序也是原来的同事写的,所以就尽量小的改动源程序。所以在原来的自定义TabHost的基础上进行更改,如下:

[code lang="xml"] <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:background="@drawable/main_background" android:orientation="horizontal" > <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabWidget android:layout_marginTop="0dp" android:layout_marginLeft="0dp" android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="68dip" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="7"> </FrameLayout> </LinearLayout> <!--在这里加一个Button --> <Button android:id="@+id/btn_add" android:layout_width="40dip" android:layout_height="40dip" android:background="@drawable/jiahao" android:layout_marginLeft="700dip" android:layout_marginTop="15dip"/> </TabHost> </LinearLayout> [/code]


在主Activity中更改onCtreate:

[java] @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); mTabHost = getTabHost(); initbacklist(); //加载黑名单; initwhitelist(); //加载白名单; firewallset(); //加载设置界面 //设置没个标题的宽度,这样Tab标题就集中在了左侧 TabWidget tabget0 = mTabHost.getTabWidget(); tabget0.getChildAt(0).getLayoutParams().width = 178; tabget0.getChildAt(1).getLayoutParams().width = 178; tabget0.getChildAt(2).getLayoutParams().width = 178; } [/java]


这样就让Tab的标题集中在左边,右侧添加了一个公共的按钮。下一次介绍如何使用公共的按钮。


推荐阅读