在Android中创建更小的标签

时间:2010-08-21 20:55:44

标签: android user-interface android-tabhost

我正在尝试在android中创建较小的标签 - 但我似乎无法让它工作,因为当我创建一个较小的标签时发生的一切是它显示更大的标签 - 但没有可绘制的。

现在这是我的标签布局代码 - 但由于某些原因,高度不是包装 - 它只是Android的平常布局高度。

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
        </FrameLayout>
    </LinearLayout>
</TabHost>

如果有人可以帮我创建像Facebook应用程序这样的东西会很棒 - 我认为看起来非常干净,我很乐意实现类似的东西:

2 个答案:

答案 0 :(得分:7)

这比我想象的要复杂得多,但是,这应该让你得到你想要的外观的基本实现......

 TabHost             host       = getTabHost();
 TabSpec             spec       = null;
 TextView            tab1       = null,
                     tab2       = null;
 Intent              intent     = null;
 Resources           resources  = getResources();
 XmlResourceParser   parser     = null;
 ColorStateList      text       = null;
 StateListDrawable[] drawables  = new StateListDrawable[2];
 int[]               selected   = {STATE_SELECTED},
                     unselected = {STATE_UNSELECTED};
 Color               selectedColor = Color.argb(255, 255, 255, 255),
                     defaultColor  = Color.argb(255, 119, 119, 119);

 // Load the colour lists.
 parser = resources.getXml(R.color.tab_text);
 text   = ColorStateList.createFromXml(getResources(), parser);

 // Add an initial tab.
 ...Create Tab Contents Here...
 spec = host.newTabSpec("tab1");
 tab1 = new TextView(this);
 tab1.setText(R.string.all_tab_title);
 tab1.setGravity(android.view.Gravity.CENTER);
 tab1.setTextSize(18.0f);
 tab1.setTextColor(text);
 spec.setIndicator(tab1);
 spec.setContent(intent);
 host.addTab(spec);

 // Add a second tab.
 ...Create Tab Contents Here...
 spec = host.newTabSpec("tab2");
 tab2 = new TextView(this);
 tab2.setText(R.string.category_tab_title);
 tab2.setGravity(android.view.Gravity.CENTER);
 tab2.setTextSize(18.0f);
 tab2.setTextColor(text);
 spec.setIndicator(tab2);
 spec.setContent(intent);
 host.addTab(spec);

 // Set the background drawable for the tabs and select the first tab.
 drawables[0] = new StateListDrawable();
 drawables[0].addState(selected, new ColorDrawable(selectedColor));
 drawables[0].addState(unselected, new ColorDrawable(defaultColor));
 drawables[1] = new StateListDrawable();
 drawables[1].addState(selected, new ColorDrawable(selectedColor));
 drawables[1].addState(unselected, new ColorDrawable(defaultColor));
 tab1.setBackgroundDrawable(drawables[0]);
 tab2.setBackgroundDrawable(drawables[1]);
 host.setCurrentTab(0);

这不会考虑制表符边框或元素之间的间距。您还需要像./res/color目录中的以下颜色状态列表定义......

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_selected="true" android:color="#ff000000" />
   <item android:state_selected="false" android:color="#ffaaaaaa" />
   <item android:color="#ffffffff"/>
</selector>

希望有所帮助。

答案 1 :(得分:3)

在另一个论坛上看到了这个,但我想在这里传递它。


TabHost th = getTabHost();
....
// Setup all the tabs -- in my case, with text only -- no icons
....
int iCnt = th.getTabWidget().getChildCount();
for(int i=0; i<iCnt; i++)
  th.getTabWidget().getChildAt(i).getLayoutParams().height /= 2;  // Or the size desired