2017-04-19 52 views
1

我试着去设计一个自定义TabWidget与FragentTabHost以下两个指南的标签布局:安卓:FragmentTabHost窗口小部件不显示名称

https://maxalley.wordpress.com/2014/09/08/android-styling-a-tab-layout-with-fragmenttabhost-and-fragments/

http://joshclemm.com/blog/custom-android-tabs/

林建设FragmentTabHost有三个片段中,第一个片段具有可扩展的列表视图。

一切工作正常,我可以设置backgroundcolor,图标(如果我想)和指标工作以及分频器(如果我想)但标题出于某种原因不出现和我无法找到它不显示的原因。

如果我从安卓删除tab_indicator.xml:背景tab_layout.xml =“@绘制/ tab_indicator”和设置的backgroundColor到任何颜色的标签标题显示出来,但后来我obviuosly不明白指标。

有没有人有线索什么是错的,什么即时消失。我试图解决这个问题,并在几天内搜索,并开始变得非常沮丧,因为它看起来很简单。

tab_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/tab_indicator"> 
    <TextView 
     android:id="@+id/tabText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:gravity="center_vertical|center_horizontal" 
     android:textColor="@drawable/tab_text" 
     android:textSize="15dp" 
     /> 
</LinearLayout> 

tab_indicator.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <!-- Non focused states--> 
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" /> 
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" /> 
    <!-- Focused states--> 
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" /> 
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" /> 
    <!-- Pressed--> 
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_pressed" /> 
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_pressed" /> 
</selector> 

activity_main.xml中

<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:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/tabHost" 
     android:layout_gravity="center_horizontal" 
     > 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      > 
      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="35dp" 
       android:showDividers="middle" 
       android:layout_weight="0" 
       android:orientation="horizontal" 
       /> 
      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       > 
      </FrameLayout> 
     </LinearLayout> 
    </android.support.v4.app.FragmentTabHost> 
</LinearLayout> 

tab_selected.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 


    <item android:top="-7dp" android:bottom="0dp" android:left="-7dp" android:right="-7dp"> 
     <shape 
      android:shape="rectangle"> 
      <padding android:left="10dp" 
       android:top="15dp" 
       android:right="10dp" 
       android:bottom="15dp" /> 
      <stroke android:width="4dp" android:color="#FF00ccff" /> 
      <solid android:color="#2fff00" 
       /> 
     </shape> 
    </item> 
</layer-list> 

回答

0

解决了这个问题,如果我改变的android:layout_height在FragmentTabHost和TabWidget它显示的文本,就这么简单。但在我看来,高度是大^^我希望它像35dp一样小,如果我改变填充tab_selected.xml,_unselected和_pressed,然后我可以改变TabWidget activity_main.xml中的高度否则文本将不适合选项卡。