2
       TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, NewUserActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();

    spec = tabHost.newTabSpec("New User").setIndicator("New User").setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, ExistingUserActivity.class);
    spec = tabHost.newTabSpec("Existing User").setIndicator("Existing User").setContent(intent);
    tabHost.addTab(spec);

    tabHost.getTabWidget()
            .getChildAt(0)
            .setLayoutParams(
                    new LinearLayout.LayoutParams((width / 2) - 2, 40));
    tabHost.getTabWidget()
            .getChildAt(1)
            .setLayoutParams(
                    new LinearLayout.LayoutParams((width / 2) - 2, 40));
    TabWidget tw = getTabWidget();

    tw.getChildAt(0).setBackgroundColor(Color.parseColor("#800000"));
    tw.getChildAt(1).setBackgroundColor(Color.parseColor("#FF6347"));

}

@Override
public void onTabChanged(String x) {

}

上面的代码初始化了 TabHost 。它使用两个选项卡。一个新用户和另一个现有用户。单击其中任何一个时,我希望该选项卡被聚焦并且颜色与另一个选项卡不同。我不知道如何使用 OnTabChanged 方法。

4

2 回答 2

4

单击其中任何一个时,我希望该选项卡被聚焦并且颜色与另一个选项卡不同。

您可以按如下方式实现它。

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            public void onTabChanged(String str) {
                tabHost.getCurrentTabView().setBackgroundColor(color);
                tabHost.getCurrentTabView().setBackgroundDrawable(drawable);
                tabHost.getCurrentTabView().setBackgroundResource(resid);
            }
        });
于 2012-06-12T12:56:23.210 回答
0

要设置具有不同状态的选项卡,请创建以下两种方法:

private void setupTab(final View view, final String tag, Intent intent) {
    View tabview = createTabView(tabHost.getContext(), tag);
    TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
    tabHost.addTab(tabSpec);
}

private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);      
    tv.setText(text);
    tv.setTypeface(tf);
    return view;
}

然后在布局文件夹中创建 tabs_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
   <TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
      android:layout_height="wrap_content" android:text="Title"
      android:textSize="15dip" android:textColor="@drawable/tab_text_selector" />
</LinearLayout>

现在在drawable文件夹中,创建xml文件。tab_bg_selector.xml、tab_bg_selected.xml 和 tab_bg_unselected.xml:

--- 选项卡选择器 ---

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--  Active tab -->
<item android:state_selected="true" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
<!--  Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
<!--  Pressed tab -->
<item android:state_pressed="true" android:drawable="@drawable/tab_bg_selected" />
<!--  Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
</selector>

--- 选项卡选择 ---

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
    android:endColor="#696969" android:angle="-90" />
</shape>

--- 选项卡未选择 ---

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#5C5C5C" android:centerColor="#424242"
    android:endColor="#222222" android:angle="-90" />
</shape>

并将颜色设置为您想要的任何颜色,祝您好运!哦,我差点忘了,在 tabhost 中设置 Intent 时,使用 setupTab 方法如下:

    Intent i = new Intent(this, AAACalendarActivity.class);
    setupTab(new TextView(this), "Month", i);
    i = new Intent(this, AAACalendarWeekActivity.class);
    setupTab(new TextView(this), "Week", i);
    i = new Intent(this, AAACalendarDayActivity.class);
    setupTab(new TextView(this), "Day", i);  
于 2012-06-12T13:06:06.763 回答