【发布时间】:2013-09-06 00:48:53
【问题描述】:
我为单选按钮设置了一个自定义图标。我已经创建了它的 png,以便它正确地以 Android 4 为中心。“中心”是指相对于其他具有标准图标的单选按钮。但是,自定义图标在 Android 2.3 上显示偏离中心。请看图片。带有修改图标的单选按钮是第二个。
如何为可绘制图标(或任何其他解决方案)设置左填充,以使其相对于标准单选按钮水平居中?
代码的相关部分如下。我将单选按钮添加到 Java 代码中的 radioGroup 中,而不是 XML 中。 radioGroup 已经被初始化。 radioButton 是一个包含单选按钮的数组。 i 是一个循环变量(我省略了循环)。带有自定义图标的单选按钮是满足if 的单选按钮。那些不满足该条件的按钮会留下标准图标。
radioButton[i].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height_pixels));
radioButton[i].setText(newNameArray[i]);
radioButton[i].setTextColor(MainActivity.colorTextNormal);
radioButton[i].setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {search(false);}
});
if (!containsSearchTermMark(baseStringArray[i])) {
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.radio_nosearch_icon, typedValue, true); // Resolve the XML: get the drawable it represents
radioButton[i].setButtonDrawable(typedValue.resourceId); // 32 dp icon, so that it takes up the same space as the original radio icon
}
radioGroup.addView(radioButton[i]);
【问题讨论】:
标签: android radio-button