如何在使用TabHost时删除像素错误

时间:2013-01-03 07:27:52

标签: android android-layout android-widget

我们对Android中的TabHost小部件有一个奇怪的问题。我们如何修复像素线错误(如下图所示):

Pixel error

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我有三个标签和一个自定义分隔符,但是自定义xml(customtabview.xml)用于所有五个标签。

Tabs.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>

答案 1 :(得分: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>

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

希望这会对你有所帮助,因为它可以帮助我。

享受编码...