选项卡未按我在Android中的需要进行自定义

选项卡未按我在Android中的需要进行自定义,android,android-tabhost,Android,Android Tabhost,对于选项卡式应用程序,我有以下活动代码 public class TabbedActivity extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCo

对于选项卡式应用程序,我有以下活动代码

public class TabbedActivity extends TabActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabbedactivity);        

        TabHost tabHost = getTabHost();         

        TabSpec photospec = tabHost.newTabSpec("RP");
        // setting Title and Icon for the Tab
        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
中定义。应用程序运行时,选项卡上的颜色仍然是默认的黑色(活动时)、灰色(不活动时)和蓝色(按下时),这是选项卡的默认android颜色。
tabmp.xml
tabrp.xml
不起作用。我在这里做错了什么吗?

需要将绘图资源设置为选项卡视图的背景。您可以执行以下两种操作之一:

  • 创建选项卡后:

     tabHost.getTabWidget().getChildAt(THE_CHILDS_POSITION_IN_THE_HOST).setBackgroundResource(R.drawable.tabmp.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="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>
    

  • 需要将图形资源设置为选项卡视图的背景。您可以执行以下两种操作之一:

  • 创建选项卡后:

     tabHost.getTabWidget().getChildAt(THE_CHILDS_POSITION_IN_THE_HOST).setBackgroundResource(R.drawable.tabmp.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="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>
    

  • 我为每个选项卡定义了一个布局。i、 当用户点击RP时,它启动intent并启动一个新的活动RP.class,我有RP.xml加载到它的onCreate上。我不能使用你的第二个选项。从第一个选项开始,在上面发布的活动中,我将在何处插入第一个解决方案?您把它搞错了。您刚才描述的不是选项卡的布局,而是tabwidget中托管的活动的布局。这与用来描述选项卡指示器如何显示的布局不同。任何一种解决方案都会奏效。如果要使用第一种解决方案,可以在调用tabhost.addtab()后放置代码。Josh Clemm有一个@Intern我很高兴你得到了它:-)我为每个选项卡定义了一个布局。i、 当用户点击RP时,它启动intent并启动一个新的活动RP.class,我有RP.xml加载到它的onCreate上。我不能使用你的第二个选项。从第一个选项开始,在上面发布的活动中,我将在何处插入第一个解决方案?您把它搞错了。您刚才描述的不是选项卡的布局,而是tabwidget中托管的活动的布局。这与用来描述选项卡指示器如何显示的布局不同。任何一种解决方案都会奏效。如果要使用第一种解决方案,可以在调用tabhost.addtab()后放置代码。Josh Clemm有一个@Intern我很高兴你得到了它:-)