创建于Android的小标签 [英] Create Smaller Tabs in Android

查看:124
本文介绍了创建于Android的小标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中创建更小的标签 - 但我似乎无法得到它的工作,因为所有当我创建一个较小的选项卡,它显示了更大的标签出现这种情况 - 但没有绘制。

I am trying to create smaller tabs in android -- but I can't seem to get it to work because all that happens when I create a smaller tab is that it shows the bigger tab -- but without a drawable.

这是我的布局code的标签了 - 但高度不包出于某种原因 - 它只是Android的通常布局高度。

This is my layout code for tabs now -- but the height isn't wrapping for some reason -- it just goes to Android's usual layout height.

<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的应用程序 - 我认为这看起来很干净,我很愿意来实现类似的东西:

It would be great if someone could help me create something like the Facebook application -- I think that looks really clean and I would love to implement something like it:

推荐答案

嗯,这比我想象它应该是,但尽管如此,这应该得到你所需的外观基本实现更为复杂...

Well this was far more complicated than I thought it should be but, nevertheless, this should get you a basic implementation of the appearance that you want...

 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目录...

This won't account for tab borders or the spacing between elements though. You also need a colour state list definition like the following in the ./res/color directory...

<?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>

希望有所帮助。

Hope that helps.

这篇关于创建于Android的小标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆