2014-02-05 31 views
0

我正在使用TabHost來保存3個片段,並創建了一個設置Tab選定/未選定背景色的方法。背景顏色使用可繪製的xml文件繪製。我得到的問題是當選擇了一個選項卡時,會根據實際的選項卡按鈕調整寬度。無論如何,他們都需要保持相同的尺寸。這將如何解決?TabHost按鈕大小錯誤

Tab buttons showing the sizing error

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) 
private void setSelectedTabColor() { 
    RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 
    rllp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    rllp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    int textSize = 22; 

    int current = mTabHost.getCurrentTab(); 
    for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) { 
     TextView txt = (TextView) mTabHost.getTabWidget().getChildAt(i) 
       .findViewById(android.R.id.title); 
     txt.setTextSize(textSize); 
     txt.setTypeface(font); 
     txt.setLayoutParams(rllp); 
     txt.setGravity(Gravity.RIGHT); 
     mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 100; 
     if (i == current) { 
      mTabHost.getTabWidget().getChildAt(i) 
        .setBackgroundResource(R.drawable.button_type1_active); 
     } else { 
      mTabHost.getTabWidget().getChildAt(i) 
        .setBackgroundResource(R.drawable.button_type1); 
      txt.setTextColor(Color.WHITE); 
     } 
    } 
} 

button_type1.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:state_pressed="true"><layer-list> 
     <item><shape> 

       <!-- Gradient Bg for Button --> 
       <gradient android:angle="270" android:endColor="@color/button_type1_pushed" android:startColor="@color/button_type1_pushed" /> 

       <stroke android:width="0.05dp" android:color="@color/button_type1_border" /> 
      </shape></item> 
    </layer-list></item> 
<item android:state_enabled="true"><layer-list> 
     <item><shape android:shape="rectangle"> 
       <gradient android:angle="90" android:endColor="@color/button_type1_normal" android:startColor="@color/button_type1_normal" /> 

       <stroke android:width="0.05dp" android:color="@color/button_type1_border" /> 
      </shape></item> 
    </layer-list></item> 

button_type1_active.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:state_pressed="true"><layer-list> 
     <item><shape> 

       <!-- Gradient Bg for Button --> 
       <gradient android:angle="270" android:endColor="@color/button_type1_pushed" android:startColor="@color/button_type1_pushed" /> 

       <stroke android:width="0.05dp" android:color="@color/button_type1_border" /> 
      </shape></item> 
    </layer-list></item> 
<item android:state_enabled="true"><layer-list> 
     <item><shape android:shape="rectangle"> 
       <gradient android:angle="90" android:endColor="@color/button_type1_active" android:startColor="@color/button_type1_active" /> 

       <stroke android:width="0.05dp" android:color="@color/button_type1_border" /> 
      </shape></item> 
    </layer-list></item> 

回答

1

這是用於解決問題的代碼。將父視圖的填充設置爲0是必要的:

private void setSelectedTabColor() { 
    RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.FILL_PARENT); 
    rllp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    rllp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    int textSize = 22; 
    final WindowManager.LayoutParams params = getWindow().getAttributes(); 

    View currentView; 
    int current = mTabHost.getCurrentTab(); 
    for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) { 
     TextView txt = (TextView) mTabHost.getTabWidget().getChildAt(i) 
       .findViewById(android.R.id.title); 
     txt.setTextSize(textSize); 
     txt.setTypeface(font); 
     txt.setLayoutParams(rllp); 
     txt.setGravity(Gravity.RIGHT | Gravity.BOTTOM); 
     currentView = mTabHost.getTabWidget().getChildAt(i); 
     LinearLayout.LayoutParams currentLayout = (LinearLayout.LayoutParams) currentView 
       .getLayoutParams(); 
     currentLayout.setMargins(0, 0, 0, 0); 
     currentView.setPadding(0, 0, 0, 0); 
     currentView.setLayoutParams(currentLayout); 
     currentView.getLayoutParams().height = 100; 
     if (i == current) { 
      mTabHost.getTabWidget().getChildAt(i) 
        .setBackgroundResource(R.drawable.button_type1_active); 
      txt.setBackgroundResource(R.drawable.button_type1_active); 
     } else { 
      mTabHost.getTabWidget().getChildAt(i) 
        .setBackgroundResource(R.drawable.button_type1); 
      txt.setBackgroundResource(R.drawable.button_type1); 
      txt.setTextColor(Color.WHITE); 
     } 
     if(i==0||i==1){ 
      txt.setPadding(0, 0, 5, 0); 
     } 
    } 
} 
0
mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 100; 

您設置的height以同樣的方式,你可以設置width到當前佈局的最大寬度。像這樣的東西應該工作:

final Display display = getWindowManager().getDefaultDisplay(); 
final Point size = new Point(); 
display.getSize(size); 

final WindowManager.LayoutParams params = getWindow().getAttributes(); 

mTabHost.getTabWidget().getChildAt(i).getLayoutParams().width = (int) params.width * 0.9; 

我想設置widthMATCH_PARENT代替WRAP_CONTENT應該工作了。