简体   繁体   中英

Can not set Image for Tab Indicator Android

I am using tabhost for my English Listening application. 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

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>

and here is the tabhost layout: 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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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