Android的标签布局教程? [英] Android Tab Layout tutorial?

查看:156
本文介绍了Android的标签布局教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在这取决于项目是否处于选中与否TabLayout控制不同的图像分配给我的标签。我跟着在Android网站上的教程,但他们只用一个形象,为它工作原理制成的例子。但它不为标签的其余部分工作。我怎样才能使它发挥作用?这是我的code:

I want to be able to assign different images to my tabs in the TabLayout control depending on whether the item is selected or not. I followed the tutorial on the Android site but they made the example with just one image and for it it works. But it doesn't work for the rest of the tabs. How can I make it work? This is my code:

主要活动:

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:

<?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>

第一个选项卡中选择:

The selector for the first tab:

<?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>

第二个选项卡中的选择:

The selector for the second tab:

<?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。

etc. My images are called ic_tab_songs_white, ic_tab_songs_grey, ic_tab_albums_white, ic_tab_albums_grey, ic_tab_artists_white, ic_tab_artists_grey.

推荐答案

难道问题是你定义选项卡的背景为你而不是你所定义的选择图像?从你的文字也不清楚你如何命名你有code例子中的两个选择文件,但你的code应该是指那些文件,而不是实际的图像。

Could the problem be that you define the tab backgrounds as the images you have instead of the selectors you have defined? From your text it is not clear how you named the two selector files you have code example for but your code should refer to those files, not to the actual images.

这篇关于Android的标签布局教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆