Android TabHost按钮大小调整错误
我使用一个TabHost来保存3个片段,并创建了一个方法来设置选项卡的选定/未选定背景色。使用可绘制的xml文件绘制背景色。我遇到的问题是,当选择一个选项卡时,宽度会根据实际的选项卡按钮进行调整。不管怎样,它们都需要保持相同的尺寸。这将如何修复Android TabHost按钮大小调整错误,android,android-fragments,tabs,android-tabhost,Android,Android Fragments,Tabs,Android Tabhost,我使用一个TabHost来保存3个片段,并创建了一个方法来设置选项卡的选定/未选定背景色。使用可绘制的xml文件绘制背景色。我遇到的问题是,当选择一个选项卡时,宽度会根据实际的选项卡按钮进行调整。不管怎样,它们都需要保持相同的尺寸。这将如何修复 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) private void setSelectedTabColor() { RelativeLayout.LayoutParams rllp = new
@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);
}
}
}
@TargetApi(Build.VERSION\u CODES.JELLY\u BEAN\u MR1)
私有void setSelectedTabColor(){
RelativeLayout.LayoutParams rllp=新的RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_内容,
ViewGroup.LayoutParams.WRAP_内容);
rllp.addRule(RelativeLayout.ALIGN\u PARENT\u RIGHT);
rllp.addRule(RelativeLayout.ALIGN\u PARENT\u BOTTOM);
int textSize=22;
int current=mTabHost.getCurrentTab();
对于(int i=0;i
按钮类型1.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>
按钮类型1\u 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>
与设置高度
的方法相同,您可以将宽度
设置为当前布局的最大宽度。像这样的方法应该会奏效:
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;
我认为将
width
设置为MATCH\u PARENT
而不是WRAP\u CONTENT
也应该有效。这是用于解决问题的代码。必须将父视图的填充设置为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);
}
}
}
private void setSelectedTabColor(){
RelativeLayout.LayoutParams rllp=新的RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_父级,
ViewGroup.LayoutParams.FILL\u父级);
rllp.addRule(RelativeLayout.ALIGN\u PARENT\u RIGHT);
rllp.addRule(RelativeLayout.ALIGN\u PARENT\u BOTTOM);
int textSize=22;
final WindowManager.LayoutParams params=getWindow().getAttributes();
查看当前视图;
int current=mTabHost.getCurrentTab();
对于(int i=0;i
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);
}
}
}