Tabhost 选项卡无法跨设备正确显示

发布于 2024-11-26 10:41:38 字数 6965 浏览 2 评论 0原文

我在不同设备上的 tabhost 中遇到奇怪的行为。这让我认为设备有问题(考虑到给我带来麻烦的是运行非官方模组),但我想无论如何我都会问。

我的选项卡位于 Horizo​​ntalScrollView 内,其目的是它们将延伸超出分配的空间,但允许用户访问它们。

这是我的代码

Activity

    // Set up Tabs
    TabHost tabs = (TabHost)findViewById(R.id.my_tabhost);
    tabs.setup();


    TabSpec tspec1 = tabs.newTabSpec("items");
    tspec1.setIndicator(makeTabView("Items", res.getDrawable(R.drawable.basket_icon_light)));
    tspec1.setContent(R.id.tab1);
    tabs.addTab(tspec1);
    TabSpec tspec2 = tabs.newTabSpec("customers");
    tspec2.setIndicator(makeTabView("Customers", res.getDrawable(R.drawable.customer_icon_light)));
    tspec2.setContent(R.id.tab2);
    tabs.addTab(tspec2);
    TabSpec tspec3 = tabs.newTabSpec("dept");
    tspec3.setIndicator(makeTabView("Departments", res.getDrawable(R.drawable.dept_icon_light)));
    tspec3.setContent(R.id.tab3);
    tabs.addTab(tspec3);
    TabSpec tspec4 = tabs.newTabSpec("users");
    tspec4.setIndicator(makeTabView("Users", res.getDrawable(R.drawable.user_icon_light)));
    tspec4.setContent(R.id.tab4);
    tabs.addTab(tspec4);
    tabs.setOnTabChangedListener(tabchange);
    TabSpec tspec5 = tabs.newTabSpec("terms");
    tspec5.setIndicator(makeTabView("Terms", res.getDrawable(R.drawable.terms_icon_light)));
    tspec5.setContent(R.id.tab5);
    tabs.addTab(tspec5);
    tabs.setOnTabChangedListener(tabchange);



private View makeTabView(String name, Drawable draw){
    View v = View.inflate(getApplicationContext(), R.layout.custom_tab, null);
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.setMargins(1, 0, 1, 0);
    ImageView image = (ImageView) v.findViewById(R.id.tab_image);
    TextView text = (TextView) v.findViewById(R.id.tab_text);
    image.setImageDrawable(draw);
    text.setText(name);
    v.setLayoutParams(lp);
    v.setBackgroundDrawable(res.getDrawable(R.drawable.tabindicator));
    return v;
}

CustomTab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal" 
  android:layout_height="match_parent" 
  android:layout_width="match_parent">
    <LinearLayout android:id="@+id/linearLayout1" 
              android:orientation="vertical" 
              android:layout_marginLeft="20dp" 
              android:layout_marginRight="20dp" 
              android:layout_height="match_parent" 
              android:layout_width="match_parent">
    <ImageView android:id="@+id/tab_image" 
               android:src="@drawable/icon" 
               android:layout_height="wrap_content" 
               android:layout_gravity="center" 
               android:scaleType="centerInside" 
               android:layout_width="wrap_content">
    </ImageView>
    <TextView android:layout_gravity="center" 
              android:layout_height="wrap_content" 
              android:id="@+id/tab_text" 
              android:text="TextView" 
              android:layout_width="wrap_content" 
              android:layout_marginBottom="10px">
    </TextView>
</LinearLayout>

TabHost.xml

    <LinearLayout android:id="@+id/contain_nav" 
                  android:layout_width="fill_parent" 
                  android:layout_height="wrap_content" 
                  android:orientation="horizontal" 
                  android:background="@drawable/border_top">
        <TabHost android:id="@+id/my_tabhost" 
                 android:layout_width="fill_parent" 
                 android:layout_height="wrap_content">
            <LinearLayout android:weightSum="100" 
                          android:layout_height="wrap_content" 
                          android:layout_width="match_parent" 
                          android:id="@+id/linearLayout1" 
                          android:orientation="vertical">
                <LinearLayout android:layout_height="wrap_content" 
                              android:layout_width="match_parent" 
                              android:id="@+id/linearLayout3">
                    <HorizontalScrollView android:layout_height="wrap_content" 
                                          android:id="@+id/horizontalScrollView2" 
                                          android:fillViewport="true" 
                                          android:layout_width="match_parent" 
                                          android:scrollbars="none">
                        <TabWidget android:orientation="horizontal" 
                                   android:id="@android:id/tabs" 
                                   android:layout_height="match_parent" 
                                   android:layout_width="wrap_content">
                        </TabWidget>
                    </HorizontalScrollView>
                </LinearLayout>
                <FrameLayout android:layout_height="0dp" 
                             android:layout_width="match_parent" 
                             android:id="@android:id/tabcontent">
                    <LinearLayout android:layout_width="match_parent" 
                                  android:layout_height="0dp" 
                                  android:id="@+id/tab1">
                    </LinearLayout>
                    <LinearLayout android:layout_width="wrap_content" 
                                  android:layout_height="0dp" 
                                  android:id="@+id/tab2">
                    </LinearLayout>
                    <LinearLayout android:layout_width="wrap_content" 
                                  android:layout_height="wrap_content" 
                                  android:id="@+id/tab3">
                    </LinearLayout>
                    <LinearLayout android:layout_width="wrap_content" 
                                  android:layout_height="wrap_content" 
                                  android:id="@+id/tab4">
                    </LinearLayout>
                    <LinearLayout android:layout_width="wrap_content" 
                                  android:layout_height="wrap_content" 
                                  android:id="@+id/tab5">
                    </LinearLayout>
                </FrameLayout>
            </LinearLayout>
        </TabHost>
    </LinearLayout>

它应该是这样的 (Mot XOOM) 这就是我不断得到的 (Viewsonic Gtablet)

我是否遗漏了一些我知道像素密度可以影响事物的渲染方式,但我只是不明白这一点。

I'm experiencing weird behavior in a tabhost across different devices. Which leads me to think there is a problem with the device (considering the one giving me trouble is running an unofficial mod) but I thought I'd ask anyway.

My tabs are inside a HorizontalScrollView with the intention that they will extend past the allotted space but allow the user to reach them.

Here's my code

Activity

    // Set up Tabs
    TabHost tabs = (TabHost)findViewById(R.id.my_tabhost);
    tabs.setup();


    TabSpec tspec1 = tabs.newTabSpec("items");
    tspec1.setIndicator(makeTabView("Items", res.getDrawable(R.drawable.basket_icon_light)));
    tspec1.setContent(R.id.tab1);
    tabs.addTab(tspec1);
    TabSpec tspec2 = tabs.newTabSpec("customers");
    tspec2.setIndicator(makeTabView("Customers", res.getDrawable(R.drawable.customer_icon_light)));
    tspec2.setContent(R.id.tab2);
    tabs.addTab(tspec2);
    TabSpec tspec3 = tabs.newTabSpec("dept");
    tspec3.setIndicator(makeTabView("Departments", res.getDrawable(R.drawable.dept_icon_light)));
    tspec3.setContent(R.id.tab3);
    tabs.addTab(tspec3);
    TabSpec tspec4 = tabs.newTabSpec("users");
    tspec4.setIndicator(makeTabView("Users", res.getDrawable(R.drawable.user_icon_light)));
    tspec4.setContent(R.id.tab4);
    tabs.addTab(tspec4);
    tabs.setOnTabChangedListener(tabchange);
    TabSpec tspec5 = tabs.newTabSpec("terms");
    tspec5.setIndicator(makeTabView("Terms", res.getDrawable(R.drawable.terms_icon_light)));
    tspec5.setContent(R.id.tab5);
    tabs.addTab(tspec5);
    tabs.setOnTabChangedListener(tabchange);



private View makeTabView(String name, Drawable draw){
    View v = View.inflate(getApplicationContext(), R.layout.custom_tab, null);
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.setMargins(1, 0, 1, 0);
    ImageView image = (ImageView) v.findViewById(R.id.tab_image);
    TextView text = (TextView) v.findViewById(R.id.tab_text);
    image.setImageDrawable(draw);
    text.setText(name);
    v.setLayoutParams(lp);
    v.setBackgroundDrawable(res.getDrawable(R.drawable.tabindicator));
    return v;
}

CustomTab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal" 
  android:layout_height="match_parent" 
  android:layout_width="match_parent">
    <LinearLayout android:id="@+id/linearLayout1" 
              android:orientation="vertical" 
              android:layout_marginLeft="20dp" 
              android:layout_marginRight="20dp" 
              android:layout_height="match_parent" 
              android:layout_width="match_parent">
    <ImageView android:id="@+id/tab_image" 
               android:src="@drawable/icon" 
               android:layout_height="wrap_content" 
               android:layout_gravity="center" 
               android:scaleType="centerInside" 
               android:layout_width="wrap_content">
    </ImageView>
    <TextView android:layout_gravity="center" 
              android:layout_height="wrap_content" 
              android:id="@+id/tab_text" 
              android:text="TextView" 
              android:layout_width="wrap_content" 
              android:layout_marginBottom="10px">
    </TextView>
</LinearLayout>

TabHost.xml

    <LinearLayout android:id="@+id/contain_nav" 
                  android:layout_width="fill_parent" 
                  android:layout_height="wrap_content" 
                  android:orientation="horizontal" 
                  android:background="@drawable/border_top">
        <TabHost android:id="@+id/my_tabhost" 
                 android:layout_width="fill_parent" 
                 android:layout_height="wrap_content">
            <LinearLayout android:weightSum="100" 
                          android:layout_height="wrap_content" 
                          android:layout_width="match_parent" 
                          android:id="@+id/linearLayout1" 
                          android:orientation="vertical">
                <LinearLayout android:layout_height="wrap_content" 
                              android:layout_width="match_parent" 
                              android:id="@+id/linearLayout3">
                    <HorizontalScrollView android:layout_height="wrap_content" 
                                          android:id="@+id/horizontalScrollView2" 
                                          android:fillViewport="true" 
                                          android:layout_width="match_parent" 
                                          android:scrollbars="none">
                        <TabWidget android:orientation="horizontal" 
                                   android:id="@android:id/tabs" 
                                   android:layout_height="match_parent" 
                                   android:layout_width="wrap_content">
                        </TabWidget>
                    </HorizontalScrollView>
                </LinearLayout>
                <FrameLayout android:layout_height="0dp" 
                             android:layout_width="match_parent" 
                             android:id="@android:id/tabcontent">
                    <LinearLayout android:layout_width="match_parent" 
                                  android:layout_height="0dp" 
                                  android:id="@+id/tab1">
                    </LinearLayout>
                    <LinearLayout android:layout_width="wrap_content" 
                                  android:layout_height="0dp" 
                                  android:id="@+id/tab2">
                    </LinearLayout>
                    <LinearLayout android:layout_width="wrap_content" 
                                  android:layout_height="wrap_content" 
                                  android:id="@+id/tab3">
                    </LinearLayout>
                    <LinearLayout android:layout_width="wrap_content" 
                                  android:layout_height="wrap_content" 
                                  android:id="@+id/tab4">
                    </LinearLayout>
                    <LinearLayout android:layout_width="wrap_content" 
                                  android:layout_height="wrap_content" 
                                  android:id="@+id/tab5">
                    </LinearLayout>
                </FrameLayout>
            </LinearLayout>
        </TabHost>
    </LinearLayout>

And here's what it should look like
(Mot XOOM)
Here's what I keep getting
(Viewsonic Gtablet)

Am I missing something I know pixel density can affect how things render, but I just don't get this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱影子 2024-12-03 10:41:39

我也有同样的问题。
这与屏幕密度有关。在为客户创建图像的同一图层上创建选项卡背景图像。
我不喜欢为此目的设计图标和图像资源,但我已将下面的链接提供给喜欢图形的人,他做得很好。

http://developer.android.com/guide/practices/ui_guidelines/icon_design_tab.html

I had the same problem.
Its matter of screen density. Create tabs background images on the same layer you did the image for Customers.
I'm not into designing icons and image resources for this purpose, but I have given the link below to someone who is into graphics and he did the job well.

http://developer.android.com/guide/practices/ui_guidelines/icon_design_tab.html

金橙橙 2024-12-03 10:41:39

这实际上是 honeycomb 3.0 和水平滚动视图的问题。这似乎在 3.1 及更高版本中已修复。我使用的方法是删除布局中的滚动视图并将其放入layout-mdpi-v11文件夹中,然后将带有滚动视图的xml布局放入layout-mdpi-v12中。

这不是最好的解决方案,但截至撰写本文时,3.0 仅覆盖了 0.2% 的活动设备。

It's actually problem with honeycomb 3.0 and the horizantalscrollview. This seems to be fixed with 3.1 and higher. I used the method of removing the scroll view in a layout and putting it in layout-mdpi-v11 folder and then putting the xml layout with the scroll view in layout-mdpi-v12.

Not the best work around but as of the time of this writing 3.0 only covers 0.2% of active devices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文