首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android标签布局教程?

Android标签布局教程?
EN

Stack Overflow用户
提问于 2010-10-12 21:52:20
回答 1查看 3.2K关注 0票数 0

我希望能够根据项目是否被选中,将不同的图像分配给TabLayout控件中的选项卡。我在Android网站上遵循了教程,但他们只使用了一张图像作为示例,因此它可以正常工作。但它不适用于其他选项卡。我怎么才能让它工作呢?这是我的代码:

主要活动:

代码语言:javascript
复制
public class Main extends TabActivity {
    private Resources res;
    private TabHost tabHost;
    private TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    private Intent intent;  // Reusable Intent for each tab

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The IDs in main2 should be exactly like the current ones.
        setContentView(R.layout.main2);

        res = getResources(); // Resource object to get Drawables
        tabHost = getTabHost();  // The activity TabHost
        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Tab1.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                          res.getDrawable(R.drawable.ic_tab_artists_grey))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, Tab2.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                          res.getDrawable(R.drawable.ic_tab_albums_grey))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Tab3.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                          res.getDrawable(R.drawable.ic_tab_songs_grey))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(1);
    }
}

主要的xml:

代码语言: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"
        android:padding="5dp">
        <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"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

第一个选项卡的选择器:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ic_tab_artists_grey"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/ic_tab_artists_white"/>
</selector>

第二个选项卡的选择器:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ic_tab_songs_grey"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/ic_tab_songs_white"/>
</selector>

我的图片被称为ic_tab_songs_white,ic_tab_songs_grey,ic_tab_albums_white,ic_tab_albums_grey,ic_tab_artists_white,ic_tab_artists_grey。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-12 23:42:42

会不会是因为您将选项卡背景定义为您拥有的图像,而不是您定义的选择器?从您的文本中不清楚您如何命名您的代码示例的两个选择器文件,但您的代码应该引用这些文件,而不是实际的图像。

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

https://stackoverflow.com/questions/3915425

复制
相关文章

相似问题

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