无法为Tab指标Android设置图像

时间:2015-08-06 07:38:16

标签: java android xml

我正在使用tabhost作为我的英语听力应用程序。我需要为我的标签指示器添加图像,但它没有工作

我有2个布局:easy.xml和normal.xml(课程级别) 我有一个tabhost布局,其中包括2个以上布局

easy.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#66CCFF"
    android:id="@+id/Easy">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewEasy"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

normal.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#66CCFF"
    android:id="@+id/Normal">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewNormal"
        android:layout_gravity="center_horizontal" />


</LinearLayout>

这是tabhost布局: activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TabHost
        android:id="@+id/TabHost01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <!-- TabWidget used to creat tabs -->
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <!-- container of tabs -->
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <!-- tab N°1 -->
                <!-- Inclusion of the interface -->
                <include
                    android:id="@+id/Easy"
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    layout="@layout/easy" >
                </include>

                <!-- tab N°2 -->
                <!-- Inclusion of the interface -->
                <include android:id="@+id/Normal"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    layout="@layout/normal" >
                </include>

            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

这是我的测试活动: TestActitvity.java

public class TestActivity extends Activity
{
    private TabHost mytabhost;

@Override protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        // Recuperation du TabHost
        mytabhost =(TabHost) findViewById(R.id.TabHost01);

        // Before adding tabs, it is imperative to call the method setup()
        mytabhost.setup();

        // Adding tabs
        mytabhost.addTab(mytabhost.newTabSpec("tab_easy").setIndicator("Easy", ContextCompat.getDrawable(this, R.drawable.abc_ab_share_pack_mtrl_alpha)).setContent(R.id.Easy));
        mytabhost.addTab(mytabhost.newTabSpec("tab_normal").setIndicator("Normal",ContextCompat.getDrawable(this, R.drawable.abc_btn_borderless_material)).setContent(R.id.Normal));


        //*************** my code to add image ***************
        mytabhost.getChildAt(0).setBackgroundResource(R.drawable.tabhost00);
        mytabhost.getChildAt(1).setBackgroundResource(R.drawable.tabhost10);

       ...
       ...
       ...
    }
}

任何人都有帮助!提前致谢

1 个答案:

答案 0 :(得分:0)

I think you can read this: Icon in Tab is not showing up

的onclick听众

在我看来这是同样的问题,不是吗?