2013-08-30 29 views
6

現在、私は自分のプロジェクトにFragmentTabHostを実装しようとしています。私はまだこの断片では新しいですが、私はレイアウトを再利用するという点でそれが非常に優れていることなどを知っています。今、私は断片でタブを作成する方法についてのチュートリアルを読んで、私はこのチュートリアルに来た:FragmentTabHostをカスタマイズTabWidgetを下に設定

http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/

今これはtabWidgetが、私はそれを望んでいた私のレイアウトの上にあることを除いて、うまく動作します一番下にある。私はtabWidgetが初期化されているすべてのタブの後に私はこれらのコードを追加しようとした私は、セットアップに必要なことを見つける:今、この1つは、すでに仕切りを排除し、色が変化しますが、明らかに私のウィジェットを入れていないだろう

mTabWidget = (TabWidget) findViewById(android.R.id.tabs); 

    mTabWidget.setBackgroundColor(Color.WHITE); 
    mTabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); 
    mTabWidget.setGravity(Gravity.BOTTOM); 

私のレイアウトの下部にあります。今私はそれをどのようにしますか?

Tabhost xmlを編集して、TabWidgetをFrameLayoutの後に置いてみましたが、何も起こりません。ここでのXMLです:

<android.support.v4.app.FragmentTabHost 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@android:id/tabhost" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

      <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical" > 

       <FrameLayout 
         android:id="@+id/tabFrameLayout" 
         android:layout_width="match_parent" 
         android:layout_height="0dp" 
         android:layout_weight="1" /> 

       <TabWidget 
         android:id="@android:id/tabs" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="0" 
         android:orientation="horizontal" 
         /> 

      </LinearLayout> 

     </android.support.v4.app.FragmentTabHost> 

答えて

13

私は、このリンクにgithub example

を参照しています。これは、あなたのレイアウトになります:カスタムタブの

<?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" > 

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" /> 

    <android.support.v4.app.FragmentTabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0" /> 
    </android.support.v4.app.FragmentTabHost> 

</LinearLayout> 

mTabHost.addTab(setIndicator(mTabHost.newTabSpec("Tab1"), 
         R.drawable.image1), 

    public TabSpec setIndicator(Context ctx,TabSpec spec, int resid) { 
     // TODO Auto-generated method stub 
     View v = LayoutInflater.from(ctx).inflate(R.layout.tabs_text, null); 
     v.setBackgroundResource(resid); 
     TextView text = (TextView) v.findViewById(R.id.tab_title); 
     text.setText(spec.getTag()); 
     return spec.setIndicator(v); 
    } 

編集

//To set drawable to your perticular TAB 
mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_login); 

エンド編集

あなたが描画可能(セレクター)を追加したい場合は

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/tab_compose_h" android:state_selected="true"/> 
    <item android:drawable="@drawable/tab_compose_h" android:state_pressed="true"/> 
    <item android:drawable="@drawable/tab_compose"/> 

</selector> 
+0

現在、それをチェックアウト。 :) – KaHeL

+0

こんにちは、それをテストし、うまく動作します!しかし、私には1つの問題があります。 tabDividersが表示され、私はそれをオフにすることを知らない。タブの色を変更することはできません。私はどうしたらいいですか? – KaHeL

+0

そして私はすでにそれを手に入れました! :)) どうもありがとうございます! – KaHeL

関連する問題