【问题标题】:Create Smaller Tabs in Android在 Android 中创建更小的选项卡
【发布时间】:2010-08-21 20:55:43
【问题描述】:

我正在尝试在 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 应用程序的东西,那就太好了——我认为它看起来很干净,我很想实现类似的东西:

【问题讨论】:

    标签: android user-interface android-tabhost


    【解决方案1】:

    嗯,这比我想象的要复杂得多,但是,这应该可以为您提供所需外观的基本实现...

     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>
    

    希望对您有所帮助。

    【讨论】:

    • 哇,这绝对比我想要的要多得多,我想我会接受这个答案,直到有人发布更可靠的方式。
    • STATE_SELECTED 和 STATE_UNSELECTED 应该是什么的 id?
    • @pakore - 很抱歉忽略了这些定义。 STATE_SELECTED 被定义为等于 android.R.attr.state_selected。 STATE_UNSELECTED 等于 STATE_SELECTED * -1。
    • @Jay - 我似乎无法评论你的答案,所以我将把我的解决方案放在这里。我所做的是使用具有三个级别的嵌套视图。外层提供了标签间的间距。中间层提供了边界。内部级别提供了实际的选项卡内容。所有这三个组合在哪里制作整个选项卡控件。再说一次,我知道有点复杂。希望这是有道理的。
    【解决方案2】:

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

    
    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&ltiCnt; i++)
      th.getTabWidget().getChildAt(i).getLayoutParams().height /= 2;  // Or the size desired
    

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多