简体   繁体   English

无法为Android标签指示器设置图像

[英]Can not set Image for Tab Indicator Android

I am using tabhost for my English Listening application. 我正在为我的英语听力应用程序使用tabhost。 And I need to add image for my tab indicator but it didn't work 我需要为选项卡指示器添加图像,但是没有用

I have 2 layout: easy.xml and normal.xml (level of the lessons) And I have a tabhost layout that includes 2 above layouts 我有2个布局:easy.xml和normal.xml(课程级别),并且我有一个Tabhost布局,其中包括2个上述布局

easy.xml 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 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>

and here is the tabhost layout: activity_test.xml 这是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>

Here is my Test Activity: TestActitvity.java 这是我的测试活动: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);

       ...
       ...
       ...
    }
}

Anyone help! 有人帮忙! Thanks in advance 提前致谢

I think you can read this: Icon in Tab is not showing up 我认为您可以阅读以下内容:“ 标签”中的图标未显示

Looks to me it is the same problem,isn't it? 在我看来这是同样的问题,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM