在Android标签中的标签中添加额外文字

问题描述 投票:0回答:1

我想在Android中的Tab中的红色圆圈内的Header中添加一些文本。要制作Tab我使用了以下代码。

my activity.Java

public class MainActivity extends TabActivity implements OnTabChangeListener {
    TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost.TabSpec spec;

        tabHost = getTabHost();
        // Android tab
        Intent intentAndroid = new Intent().setClass(this, Aclass.class);
        spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
                .setIndicator("My Profile ");
        tabHost.addTab(spec);
        tabHost.setOnTabChangedListener(this);

        // Welcome tab
        Intent intentBus = new Intent().setClass(this, Bclass.class);
        spec = tabHost.newTabSpec("Welcome").setIndicator("Card View")
                .setContent(intentBus);
        tabHost.addTab(spec);

        tabHost.getTabWidget().getChildAt(1)
                .setBackgroundResource(R.drawable.two);

        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0)
                .setBackgroundResource(R.drawable.one);

    }

    @Override
    public void onTabChanged(String tabId) {

        Log.i("tabs", "CurrentTab: " + tabHost.getCurrentTab());

        if (tabHost.getCurrentTab() == 0) {
            tabHost.getTabWidget().getChildAt(0)
                    .setBackgroundResource(R.drawable.one);
            tabHost.getTabWidget().getChildAt(1)
                    .setBackgroundResource(R.drawable.two);
        }

        if (tabHost.getCurrentTab() == 1) {
            tabHost.getTabWidget().getChildAt(0)
                    .setBackgroundResource(R.drawable.one_hover);
            tabHost.getTabWidget().getChildAt(1)
                    .setBackgroundResource(R.drawable.two_hover);
        }

    }
}

XMl代码是

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:tabStripEnabled="false"
            android:showDividers="none"
            />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>
android tabs tabwidget
1个回答
1
投票

在layoutInflater的帮助下使用这样的---------

 TabSpec passSpec = tabs.newTabSpec("Pass Tab"); 
    passSpec.setIndicator("Passengers", getResources().getDrawable(R.drawable.tab_message));

    passSpec.setContent(new TabHost.TabContentFactory() { 
        public View createTabContent(String tag) { 
            View layout = mInflater.inflate(R.layout.tab_content_passengers, null);                 
            return(layout); 
        } 
    });
    tabs.addTab(passSpec);
© www.soinside.com 2019 - 2024. All rights reserved.