3

我们有一个关于 Android 中的 TabHost-widget 的奇怪问题。我们如何修复像素线错误(如下图所示):

像素错误

谢谢你的帮助。

4

2 回答 2

0

几天前我也有同样的问题。但是现在有了将其更改为图像的解决方案。默认情况下,选择了第一个选项卡,当“获取”更改时,所有选项卡会根据选项卡选择自动更改。

请参见下面的代码:

   public class MainTabActivity extends TabActivity {
    TabHost mTabHost;
    TabWidget mTabWidget;
    private TextView title;

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

        //Resources res = getResources();
        mTabHost = getTabHost();
        TabHost.TabSpec spec;
        mTabWidget = mTabHost.getTabWidget();
        Intent intent;

        intent = new Intent().setClass(MainTabActivity.this, TodaysDealsGroupActivity.class);
        spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_todays_deal)).setIndicator(getResources().getString(R.string.tab_todays_deal), getResources().getDrawable(R.drawable.tab_loyalshop_selector)).setContent(intent);
        mTabHost.addTab(spec);

        intent = new Intent().setClass(MainTabActivity.this, BuddiesGroupActivity.class);
        spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_buddies)).setIndicator(getResources().getString(R.string.tab_buddies), getResources().getDrawable(R.drawable.tab_buddies_selector)).setContent(intent);
        mTabHost.addTab(spec);

        intent = new Intent().setClass(MainTabActivity.this, SearchGroupActivity.class);
        spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_search)).setIndicator(getResources().getString(R.string.tab_search), getResources().getDrawable(R.drawable.tab_search_selector)).setContent(intent);
        mTabHost.addTab(spec);

        intent = new Intent().setClass(MainTabActivity.this, ProfileGroupActivity.class);
        spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_profile)).setIndicator(getResources().getString(R.string.tab_profile), getResources().getDrawable(R.drawable.tab_profile_selector)).setContent(intent);
        mTabHost.addTab(spec);

        intent = new Intent().setClass(MainTabActivity.this, NotificationsGroupActivity.class);
        spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_notifications)).setIndicator(getResources().getString(R.string.tab_notifications), getResources().getDrawable(R.drawable.tab_notification_selector)).setContent(intent);
        mTabHost.addTab(spec);

        for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
            mTabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_black_bg));
            title = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title);
            title.setTextColor(Color.WHITE);
            title.setTextSize(10);
        }

        // check if App starts from the Notification click or not
        if(getIntent().hasExtra("notification")){
            // for the current tab selection
            mTabHost.getTabWidget().getChildAt(4).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg));
            mTabHost.setCurrentTab(4);
            title = (TextView) mTabWidget.getChildAt(4).findViewById(android.R.id.title);

        }else{
            // for the current tab selection
            mTabHost.getTabWidget().getChildAt(0).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg));
            mTabHost.setCurrentTab(0);
            title = (TextView) mTabWidget.getChildAt(0).findViewById(android.R.id.title);

        }


        title.setTextColor(Color.BLACK);
        title.setTextSize(10);

        // listener for the tab changed
        mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {

                for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
                    mTabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_black_bg));
                    title = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title);
                    title.setTextColor(Color.WHITE);
                    title.setTextSize(10);
                }

                int tab = mTabHost.getCurrentTab();

                mTabHost.getTabWidget().getChildAt(tab).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg));
                title = (TextView) mTabWidget.getChildAt(tab).findViewById(android.R.id.title);
                title.setTextColor(Color.BLACK);
                title.setTextSize(10);
            }
        });
    }
}

这是 XML 文件main_tab_layout.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
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="bottom|center_horizontal"
                android:tabStripEnabled="false" >
            </TabWidget>
        </LinearLayout>
    </TabHost>

</LinearLayout>

只需替换为您的图像并查看结果。

希望这会对你有很大帮助,因为它对我有帮助。

享受编码...

于 2013-01-09T03:53:32.187 回答
0

我有三个选项卡和一个自定义分隔线,但是自定义 xml (customtabview.xml) 用于所有五个选项卡。

标签.java

    tabHost = getTabHost();
    TabHost.TabSpec spec; 

    LinearLayout v = (LinearLayout)(getLayoutInflater().inflate(R.layout.customtabview,     null));

    //FIRST TAB 
    ((TextView)v.findViewById(R.id.title)).setText("FIRST");
    ((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable .tab_back,0,0 );
    spec = tabHost.newTabSpec("first").setIndicator(v).setContent(R.id.tab_first_info);
    tabHost.addTab(spec);

    //SECOND TAB
    ((TextView)v.findViewById(R.id.title)).setText("SECOND");
    ((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_back,0,0 );
    spec = tabHost.newTabSpec("second").setIndicator(v).setContent(R.id.tab_second_info);
     tabHost.addTab(spec);

    //THIRD TAB
    ((TextView)v.findViewById(R.id.title)).setText("THIRD");
    ((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_back,0,0 );
    spec = tabHost.newTabSpec("third").setIndicator(v).setContent(R.id.tab_third_info);
    tabHost.addTab(spec);

customtabview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >

        <TextView
           android:id="@+id/title"
           android:drawablePadding="4dp"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           />

</LinearLayout>
于 2013-01-08T11:22:58.767 回答