简体   繁体   English

如何在Android Tab Host中将图像添加为Tab指示器?

[英]How to add images as a tab indicator in android tab host?

I am currently developing an application which a tab host is shown, each one of tab in the tab host is an activity. 我当前正在开发一个显示标签主机的应用程序,标签主机中的每个标签都是一个活动。 Now, what I want to do is to replace my text tab indicator as an image. 现在,我要做的是将文本标签指示符替换为图像。 These are the code that I used for implementing my tab host activity. 这些是我用于实现选项卡宿主活动的代码。 TabHostActivity: TabHostActivity:

public class TabHostActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabhost);

        TabHost tabHost = (TabHost) findViewById (android.R.id.tabhost);

        TabSpec tab1 = tabHost.newTabSpec("First Tab");
        TabSpec tab2 = tabHost.newTabSpec("Second Tab");
        TabSpec tab3 = tabHost.newTabSpec("Third Tab");

        tab1.setIndicator("Tab1");
        tab1.setContent(new Intent(this, Tab1Activity.class));
        tab1.setContent(new Intent(this, Tab1Activity.class));

        tab2.setIndicator("Tab2");
        tab2.setContent(new Intent(this, Tab2Activity.class));

        tab3.setIndicator("Tab3");
        tab3.setContent(new Intent(this, Tab3Activity.class));


        tabHost.addTab(tab1);
        tabHost.addTab(tab2);
        tabHost.addTab(tab3);

    }

} }

As you can see on my code, I set Tab1, Tab2, Tab3 as my tab indicators, and want to replace this as an image 如您在代码中所见,我将Tab1,Tab2,Tab3设置为我的标签指示符,并希望将其替换为图像

This is my activity_tabhost.xml 这是我的activity_tabhost.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
          android:id="@android:id/tabhost">

        <LinearLayout 
                android:id="@+id/LinearLayout01"
                android:orientation="vertical" 
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">

                <TabWidget 
                    android:id="@android:id/tabs"
                    android:layout_height="wrap_content" 
                    android:layout_width="fill_parent">
                </TabWidget>

                <FrameLayout 
                    android:id="@android:id/tabcontent"
                    android:layout_height="fill_parent"
                     android:layout_width="fill_parent">
                </FrameLayout>

        </LinearLayout>

</TabHost>

Add images which you want to use in the tabs in the draw-able folder and get through this way. 在可绘制文件夹的选项卡中添加要使用的图像,然后通过这种方式进行操作。

tab1.setIndicator("",getResources().getDrawable(R.drawable.tab1));
tab2.setIndicator("",getResources().getDrawable(R.drawable.tab2));
tab3.setIndicator("",getResources().getDrawable(R.drawable.tab3));

试试这个可能对你有帮助

  MyTab.setIndicator("", getResources().getDrawable(R.drawable.tabicon));

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

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