首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何自定义android选项卡或背景更改?

如何自定义android选项卡或背景更改?
EN

Stack Overflow用户
提问于 2013-07-27 19:39:03
回答 1查看 35.3K关注 0票数 16

我对android非常陌生。现在我构建了一个小应用程序。

我需要改变的外观和感觉的android默认查找在2.2。因此,在这里我尝试更改选项卡的背景。你能帮我做这个吗?

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

这就是我需要的实际输出。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-27 22:29:22

您的Tab主机XML文件

TabHost

代码语言:javascript
复制
<?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

代码语言:javascript
复制
@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;
}

CustomTabsLayout tabs_bg.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>

,最后在您的主活动类

代码语言:javascript
复制
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

和finish :)

票数 45
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17897351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档