0

tabhost 위젯에서 텍스트 정렬 및 구분선 색상을 설정하는 방법.android에서 탭 색상, 구분선 및 텍스트 정렬을 설정

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 


    Bundle b = new Bundle(); 
    b.putString("key", "Category"); 
    mTabHost.addTab(mTabHost.newTabSpec("Category").setIndicator("Category"), 
      CategoryFragment.class, b); 

    b = new Bundle(); 
    b.putString("key", "Activity"); 
    mTabHost.addTab(mTabHost.newTabSpec("Activity") 
      .setIndicator("Activity"), ActivityFragment.class, b); 

    b = new Bundle(); 
    b.putString("key", "Chart"); 
    mTabHost.addTab(mTabHost.newTabSpec("Chart").setIndicator("Chart"), 
      ChartFragment.class, b); 
    mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_selector); 
    mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab_selector); 
    mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab_selector); 

    mTabHost.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE); 
    mTabHost.getTabWidget().setDividerDrawable(R.color.white); 

tab_selector의 XML -

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

    <item android:drawable="@color/brown" android:state_selected="true"/> 
    <item android:drawable="@color/black" android:state_selected="false"/> 
    <item android:drawable="@color/red"/> 

</selector> 

레이아웃 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" > 

    <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" 
     android:foreground="@color/final_red" 
     android:textAlignment="center" 
     android:layout_gravity="center_vertical" 

     android:foregroundGravity="center" > 

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

</LinearLayout> 

나는 setDividerDrawable 사용했다하지만 난 코드를 실행 한 후 구분선을 볼 수 없습니다. 또한 탭 사이에 구분선을 표시하려고합니다. 어떻게해야합니까? 첨부 된 그림에서 범주, 활동 및 차트 사이에 구분선이 필요합니다. 그리고 텍스트이 내가 FragmentTabHostTabWidget를 사용하여 TabView을 만들어 어떻게

enter image description here

+2

이 setdividerdrawble tabhost.getTabWidget() setShowDividers (TabWidget.SHOW_DIVIDER_MIDDLE) 전에 한 줄을 추가;. –

+0

체크 아웃 하시겠습니까?> –

+0

@DigveshPatel 업데이트 된 게시물 및 로그 켓 오류를 확인하십시오. –

답변

0

정렬 중심이되어야합니다. 이 시도 :

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

     <HorizontalScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:scrollbars="none" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff0000" 
       android:gravity="center" 
       android:orientation="horizontal" 
       android:scrollbars="none" 
       android:showDividers="middle" 
       android:soundEffectsEnabled="true" 
       android:splitMotionEvents="true" 
       android:tabStripEnabled="true" 
       android:textAlignment="inherit" 
       android:textDirection="inherit" /> 
     </HorizontalScrollView> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

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

편집

private FragmentTabHost mTabHost; 
DatabaseHandler db = new DatabaseHandler(this); 
SharedPreferences preferences; 
Editor editor; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    preferences = getApplicationContext().getSharedPreferences("My Loging", 
      Context.MODE_PRIVATE); 

    editor = preferences.edit(); 

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 

    mTabHost.setup(this, getSupportFragmentManager(), 
      android.R.id.tabcontent); 
    mTabHost.addTab(
      mTabHost.newTabSpec("Home").setIndicator("Home", 
        getResources().getDrawable(R.drawable.home_icon)), 
      HomeFragment.class, null); 
    mTabHost.addTab(
      mTabHost.newTabSpec("Product").setIndicator("Product", 
        getResources().getDrawable(R.drawable.home_icon)), 
      ProductFragment.class, null); 
    mTabHost.addTab(
      mTabHost.newTabSpec("MyOrder").setIndicator("MyOrder", 
        getResources().getDrawable(R.drawable.my_order_icon)), 
      MyOrderFragment.class, null); 

    mTabHost.addTab(
      mTabHost.newTabSpec("AboutUs").setIndicator("About Us", 
        getResources().getDrawable(R.drawable.history_icon)), 
      AboutUsFragment.class, null); 

    mTabHost.addTab(
      mTabHost.newTabSpec("ContactUs").setIndicator("Contact Us", 
        getResources().getDrawable(R.drawable.history_icon)), 
      ContactUsFragment.class, null); 

} 
+0

당신을위한 관련 자바 코드를 게시 할 수 탭 –

+0

편집을 확인하십시오. – Shvet

+0

탭의 경우 기본 색상을 가져옵니다. 그림과 같이 사용자 정의 배경을 추가하려면 어떻게합니까? TextAlignment center가 작동하지 않습니다. –