【发布时间】:2014-02-05 19:48:46
【问题描述】:
我正在使用 TabHost 来保存 3 个片段,并创建了一个方法来设置选项卡选择/未选择的背景颜色。背景颜色是使用可绘制的 xml 文件绘制的。我得到的问题是选择选项卡时,为实际选项卡按钮调整宽度。无论如何,它们都需要保持相同的大小。这将如何解决?
@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>
【问题讨论】:
标签: android android-fragments tabs android-tabhost