如何在Tab小部件中增加选定的标签高度?

时间:2015-04-10 09:32:11

标签: android tabwidget

  

我按照下面的代码更改了所选标签的高度。但是我并没有像我预期的那样得到。

 mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 90;
 mTabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 80;
 mTabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 80;
 mTabHost.getTabWidget().getChildAt(3).getLayoutParams().height = 80;
  

供参考,请查看下图,这是我的实际要求。   Tabwidget

1 个答案:

答案 0 :(得分:1)

  

首先创建一个xml文件bottom_tabs.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.support.v4.app.FragmentTabHost
       android:id="@android:id/tabhost"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:background="#FFFFFF" >

    <RelativeLayout
        android:id="@+id/linearLayoutTab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

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

        <FrameLayout
            android:id="@+id/realtabcontent"
            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:layout_alignParentBottom="true"
            android:background="#00000000"
            android:tabStripEnabled="true" />
    </RelativeLayout>
</android.support.v4.app.FragmentTabHost>

  

将背景图像添加到标签图标,如:

mTabHost.getTabWidget().getChildAt(0)
            .setBackgroundResource(R.drawable.d_tab_bg_selector);
  

d_tab_bg_selector代码:

<?xml version="1.0" encoding="utf-8"?>

<!-- Active tab -->
<item android:drawable="@drawable/d_pressed_state" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Inactive tab -->
<item android:drawable="@drawable/d_unpressed_state" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<!-- Pressed tab -->
<item android:drawable="@drawable/d_pressed_state" android:state_pressed="true"/>
<!-- Selected tab (using d-pad) -->
<item android:drawable="@drawable/d_unpressed_state" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

  

d_pressed_state源代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#00000000" />
    </shape>
</item>
<item
    android:top="8dp">
    <shape android:shape="rectangle">
        <solid android:color="#009343" />
    </shape>
</item>
<item android:right="1dp" android:left="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#000000" />
    </shape>
</item>

  

为未处理状态编写自己的代码。

相关问题