动态添加时按钮文本被截断
Button text cut off when added dynamically
前 5 个按钮是通过编程添加的,最后一个是通过 XML 添加的。两种方式我都使用相同的参数。为什么动态添加的按钮文本被截断?
程序化:
Button b = new Button(getActivity());
b.setText(text);
b.setAllCaps(false);
b.setBackgroundResource(R.drawable.button_tag_rect);
b.setTextColor(getResources().getColor(R.color.colorWhiteText));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
dpToPx(28));
int marginInPx = dpToPx(2);
params.setMargins(marginInPx, marginInPx, marginInPx, marginInPx);
tagCloudTwitter.addView(b, tagCloudTwitter.getChildCount()-1, params);
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_margin="2dp"
android:textAllCaps="false"
android:background="@drawable/button_tag_rect"
android:textColor="@color/colorWhiteText"
android:text="test"/>
编辑:已解决!请参阅下面我的回答。
尝试设置按钮的高度和宽度;
b.setWidth(10);
b.setHeight(100);
有趣的是,设置 "dummy" 填充有帮助:
b.setPadding(1,1,1,1);
¯\_(ツ)_/¯
前 5 个按钮是通过编程添加的,最后一个是通过 XML 添加的。两种方式我都使用相同的参数。为什么动态添加的按钮文本被截断?
程序化:
Button b = new Button(getActivity());
b.setText(text);
b.setAllCaps(false);
b.setBackgroundResource(R.drawable.button_tag_rect);
b.setTextColor(getResources().getColor(R.color.colorWhiteText));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
dpToPx(28));
int marginInPx = dpToPx(2);
params.setMargins(marginInPx, marginInPx, marginInPx, marginInPx);
tagCloudTwitter.addView(b, tagCloudTwitter.getChildCount()-1, params);
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_margin="2dp"
android:textAllCaps="false"
android:background="@drawable/button_tag_rect"
android:textColor="@color/colorWhiteText"
android:text="test"/>
编辑:已解决!请参阅下面我的回答。
尝试设置按钮的高度和宽度;
b.setWidth(10);
b.setHeight(100);
有趣的是,设置 "dummy" 填充有帮助:
b.setPadding(1,1,1,1);
¯\_(ツ)_/¯