2017-04-19 51 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中的高度否則文本將不適合選項卡。