2 myq 26 myq_26 于 2012.12.17 10:42 提问

在Android中自定义Tabs

以下的activity是选项卡式的应用程序。

public class TabbedActivity extends TabActivity {

    /** activity第一次创建时调用 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabbedactivity);        

        TabHost tabHost = getTabHost();         

        TabSpec photospec = tabHost.newTabSpec("RP");
        // 为Tab设置Title和Icon
        photospec.setIndicator("RP", getResources().getDrawable(R.drawable.tabrp));
        Intent photosIntent = new Intent(this, RP.class);
        photospec.setContent(photosIntent);

        // Tab for Songs
        TabSpec songspec = tabHost.newTabSpec("MP");
        songspec.setIndicator("MP", getResources().getDrawable(R.drawable.tabmp));
        Intent songsIntent = new Intent(this, MP.class);
        songspec.setContent(songsIntent);

        tabHost.addTab(photospec);
        tabHost.addTab(songspec);           
    }       
}

定义的tabbedactivity.xml

<?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"/>
    </LinearLayout>
</TabHost>

我添加了tabmp.xml和tabrp.xml

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

我在color.xml中定义了颜色属性。程序也能很好的执行,但是当tabs有动作时,它的颜色是默认的黑色,没有动作时,是灰色的,当点击时是蓝色的。这样看来tabmp.xml和tabrp.xml是不能正常运行的,如何改这里的代码呢?

2个回答

LiDaming_and
LiDaming_and   2012.12.19 13:33
已采纳

1.如果你创建了tab

 tabHost.getTabWidget().getChildAt(THE_CHILDS_POSITION_IN_THE_HOST).setBackgroundResource(R.drawable.tabmp.xml);

2.为tab创建一个自定义的布局

<?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="10dp" 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="15dp" android:textColor="@drawable/tab_text_selector"
        android:textStyle="bold" />
</LinearLayout>

然后创建你的tab:

View tabview = LayoutInflator.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView)view.findViewById(R.id.tabsText);
tv.setText(TEXT);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(i);
tabHost.addTab(setContent);
cytown
cytown   2012.12.17 11:16

要组合android:state_selectedandroid:state_pressedandroid:state_focused

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@      drawable/tab_unselected_v4" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@       drawable/tab_selected_v4" />

<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@       drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@        drawable/tab_focus" />

<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!