如何在Android Tab Host中将图像添加为Tab指示器? [英] How to add images as a tab indicator in android tab host?

查看:75
本文介绍了如何在Android Tab Host中将图像添加为Tab指示器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在开发一个显示标签主机的应用程序,标签主机中的每个标签都是一个活动.现在,我要做的是将文本标签指示符替换为图像.这些是我用于实现选项卡宿主活动的代码. TabHostActivity:

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:

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);

    }

}

如您在代码中所见,我将Tab1,Tab2,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

这是我的activity_tabhost.xml

This is my 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));

这篇关于如何在Android Tab Host中将图像添加为Tab指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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