如何自定义安卓标签或更改背景?

16

我对安卓开发非常陌生。现在我正在构建一个小应用程序。

我需要改变安卓2.2版本中默认的外观和感觉。因此,我尝试更改选项卡的背景。您能帮助我吗?

我喜欢使用xml / style的方式。

这是我需要的实际输出:

Tabs


https://dev59.com/4HI95IYBdhLWcg3w5iU4 - selva_pollachi
我尝试了Selva提供的实际链接,但没有得到结果。可能是我的问题。 - Sibiraj PR
@SibirajPR,请看这里,http://joshclemm.com/blog/?p=136 - yugidroid
1个回答

46

您的选项卡主机 XML 文件

TabHost 选项卡主机

<?xml version="1.0" encoding="utf-8"?> 
<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> 

在你的主活动(Main Activity)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);

    setupTab(new TextView(this), "Tab 1");
    setupTab(new TextView(this), "Tab 2");
    setupTab(new TextView(this), "Tab 3");
}

private void setupTab(final View view, final String tag) {
    View tabview = createTabView(mTabHost.getContext(), tag);
    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
        public View createTabContent(String tag) {
            return view;
        }
    });
    mTabHost.addTab(setContent);
}

private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}

自定义标签布局 tabs_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsLayout" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
    android:padding="10dip" android:gravity="center" android:orientation="vertical">

    <TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Title"
        android:textSize="15dip" android:textColor="@drawable/tab_text_selector" />
</LinearLayout>

tab_text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@android:color/white" />
    <item android:state_focused="true" android:color="@android:color/white" />
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="#f8f8f8" />
</selector>

tab_bg_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!--  Active tab -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
    <!--  Inactive tab -->
    <item android:state_selected="false" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
    <!--  Pressed tab -->
    <item android:state_pressed="true" android:drawable="@android:color/transparent" />
    <!--  Selected tab (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
    android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>

tab_bg_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
        android:endColor="#696969" android:angle="-90" />
</shape>

tab_bg_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#5C5C5C" android:centerColor="#424242"
    android:endColor="#222222" android:angle="-90" />
</shape>

最后,在您的主活动类中实现以下内容

mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

完成了 :)


这对你有帮助吗? :) - Abdulkadir NURKALEM
@SibirajPR,你能告诉我mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider)中的tab_divider是什么吗?谢谢。 - Hassaan Rabbani
TabHost 给我返回了 NullPointerException 异常.. 这个对其他人有效吗? - Dusean Singh
2
mTabHost.addTab(setContent); 处出现错误,有人知道为什么会出现 null pointer exception 吗? - Kartheek Sarabu
1
回答Kartheek的问题 = 在onCreate方法中添加mTabHost.setup();,就像这样: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTabHost = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup(); setupTab(new TextView(this), "标签1"); setupTab(new TextView(this), "标签2"); setupTab(new TextView(this), "标签3"); } - Gene
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接