android:orientation ="vertical"不适用于 TabWidget

标签 android tabwidget

我的应用程序有一个带有四个选项卡的 tabhost,现在我正在尝试为横向模式制作漂亮的布局。为了利用额外的水平空间,我想将 TabWidget 放在屏幕的右侧,当然所有选项卡都必须一个在另一个下面(就像在一列中)。 但是当切换到横向模式时,所有选项卡都排成一行,看起来很丑陋。如何解决?

Screenshot

<?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:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        >

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

            <include layout="@layout/filter_wizard"/>
            <include layout="@layout/filter_wizard"/>
            <include layout="@layout/filter_wizard"/>
            <include layout="@layout/filter_wizard"/>

            </FrameLayout>

             <TabWidget
            android:background="#75ffffff"
            android:id="@android:id/tabs"
            android:layout_width="wrap_content" android:orientation="vertical" 
            android:layout_height="fill_parent" android:layout_weight="0" />



    </LinearLayout>
</TabHost>

最佳答案

这就是我如何设置 TabHost 以在屏幕左侧显示标签,标签垂直堆叠。

需要为 Activity 设置 2 种不同的布局,一种是纵向(“正常”)模式,一种是横向模式。这意味着使用 TabActivity。

我将TabActivity 使用的布局复制到我自己的项目中,并将其命名为main_view.xml(存储在res/layout 中)。在这里:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
         android:id="@+id/tabHost"
         android:layout_width="match_parent" 
         android:layout_height="match_parent">
    <LinearLayout android:orientation="vertical"
                  android:layout_width="match_parent" 
                  android:layout_height="match_parent">
        <TabWidget android:id="@android:id/tabs"
                   android:layout_height="wrap_content" 
                   android:layout_width="match_parent"
                   android:layout_weight="0" />
        <FrameLayout android:id="@android:id/tabcontent"
                     android:layout_width="match_parent" 
                     android:layout_height="0dip"
                     android:layout_weight="1"/>
    </LinearLayout>
</TabHost>

必须重复使用 Android id tabstabcontent

在横向模式下,我通过反转所有控件的布局高度/宽度属性并将 LinearLayout 的方向设置为水平(TabWidgetFrameLayout 必须水平相邻)。这是在 res/layout-land 中的结果,也称为 main_view.xml:

<TabHost   xmlns:android="http://schemas.android.com/apk/res/android" 
           android:id="@+id/tabHost"
           android:layout_width="match_parent" 
           android:layout_height="match_parent">
    <LinearLayout android:orientation="horizontal"
                  android:layout_width="match_parent" 
                  android:layout_height="match_parent">
        <TabWidget android:id="@android:id/tabs" 
                   android:layout_height="match_parent" 
                   android:layout_width="wrap_content"
                   android:layout_weight="0" />
        <FrameLayout android:id="@android:id/tabcontent"
                     android:layout_height="match_parent" 
                     android:layout_width="0dip"
                     android:layout_weight="1"/>
    </LinearLayout>
</TabHost>

请注意,如果您想要右侧的选项卡,请在上面的 XML 中将 TabWidget 放在 FrameLayout 之后。

TabWidget 本身就是一个 LinearLayout。请注意,我没有在 XML 中设置方向。这是因为 TabWidget 在其自己的代码中执行此操作(是的,它是硬编码的)。为了解决这个问题,必须在代码中重新设置方向。这是我在 Activity 的 oncreate 中的做法

setContentView(R.layout.main_view);

final TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();

Resources res = getResources();
Configuration cfg = res.getConfiguration();
boolean hor = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE;

if (hor) {
    TabWidget tw = tabHost.getTabWidget();
    tw.setOrientation(LinearLayout.VERTICAL);
}

由于 TabHost 是通过 setContentView 创建的,因此必须显式调用其设置方法。

创建标签页的常用方法是调用:

tabHost.addTab(tabHost.newTabSpec("tab name").setIndicator("title", icon).setContent(...));

setIndicator 方法以标题字符串和可绘制对象作为参数,创建仅在纵向模式下有效的布局。必须创建自己的 View 并将其提供给 setIndicator。复制 TabSpec.LabelAndIconIndicatorStrategy.createIndicatorView 代码就足够了:

   private View createIndicatorView(TabHost tabHost, CharSequence label, Drawable icon) {

       LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

       View tabIndicator = inflater.inflate(R.layout.tab_indicator,
               tabHost.getTabWidget(), // tab widget is the parent
               false); // no inflate params

       final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
       tv.setText(label);

       final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon);
       iconView.setImageDrawable(icon);

       return tabIndicator;
   }

与原始 Google 代码的不同之处在于, View 布局本身、TextViewImageView id 来 self 们自己的应用程序,而不是 Android 内部 id。

对于纵向模式,我们可以重用 Android 中存储在 res/layout 中的 tab_indicator.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="64dip"
    android:layout_weight="1"
    android:layout_marginLeft="-3dip"
    android:layout_marginRight="-3dip"
    android:orientation="vertical"
    android:background="@drawable/tab_indicator">

    <ImageView android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
    />

    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        style="?android:attr/tabWidgetStyle"
    />

</RelativeLayout>

同样,这与原始 Android XML 相同,除了 ID。对于横向友好的版本,我们需要再次反转布局宽度和高度属性。这给了我们 res/layout-land:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="64dip"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:layout_marginTop="-3dip"
    android:layout_marginBottom="-3dip"
    android:orientation="vertical"
    android:background="@drawable/tab_indicator">

    <ImageView android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
    />

    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        style="?android:attr/tabWidgetStyle"
    />
</RelativeLayout>

(我将 marginLeft 和 marginRight 更改为 marginTop 和 marginBottom 但不确定它是否有用)

这些最后的 XML 文件引用了@drawable/tab_indicator,所以我们需要从 Android 源代码中复制它,以及 drawable/tab_selected.9.png、drawable/tab_unselected.9.png、drawable/tab_focus.9.png .

现在创建标签变为:

tabHost.addTab(tabHost.newTabSpec(AllTabName)
                .setIndicator(createIndicatorView(tabHost, "tab title", icon)))
                .setContent(this));

编辑:演示项目位于:VerticalTabHost on SkyDrive

关于android:orientation ="vertical"不适用于 TabWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4149953/

相关文章:

android - 以编程方式修改 ActionBarDrawerToggle Drawable

android - 如何最好使用 xml 为 tabwidget 按钮设置禁用状态的样式

android - (Android) 如何更改 tabWidget 中选项卡的大小?

android - 当从代码设置 qtabwidget 背景时,tabwidget 中的 qtabwidget 分隔符消失了吗?

android 在 tabbedActivity 中禁用/启用选项卡

java - 想要从Android中的Parse getCallback中获取数据

android - 在操作栏选项卡中添加 Page Curl

android - 文件 style.xml 中不支持的 Node 'item'

android - Firebase 身份验证 - 电话 - "INVALID_CERT_HASH"

android - 如何在 Tabhost android 中添加图标和图像?