【问题标题】:How can I set the tab height in a layout XML file?如何在布局 XML 文件中设置选项卡高度?
【发布时间】:2011-10-08 13:45:44
【问题描述】:

我以前用过:

tabHost.getTabWidget().getChildAt(0).getLayoutParams().height =50;

设置我的标签高度。

但我想将此选项卡高度设置从 Java 代码移动到布局 xml 文件,目的是为 layout/main.xmllayout-large/main 使用不同的高度值.xml

我的问题是如何在 xml 文件中设置标签高度?

<?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:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">


        </FrameLayout>
      </LinearLayout>
</TabHost>

【问题讨论】:

    标签: android android-layout android-emulator android-widget android-manifest


    【解决方案1】:

    您可以根据需要指定 dip 中制表符的高度。您可以使用属性窗口或直接在 .Xml 文件中给出。

              <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="50dip" />
    

    【讨论】:

      【解决方案2】:

      另外在xml中设置高度

      <TabWidget
                  android:id="@android:id/tabs"
                  android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="@dimen/tab_height" />
      

      我也在代码中做了一些动作

      int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, res.getDimension(R.dimen.tab_height), res.getDisplayMetrics());
          for (int i=0; i<mTabHost.getTabWidget().getTabCount(); i++)
          {
              mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = height;
          }
      

      这使得标签高度和蓝色带状线的位置正确。如果没有 java 代码,蓝线可能会出现在拉伸的选项卡上。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-23
        • 2021-01-20
        • 2013-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-03
        相关资源
        最近更新 更多