我有一个代码可以在 a 中动态添加按钮RelativeLayout
(MyButton 扩展了 Button 并且有一个setTag
andgetTag
来设置一个ImageView
)。这些按钮可能有ImageView
一个图标或没有。
分配 ImageView 按钮(如图标)的代码:
//如果用户使用图标设置主题
if (this.isShowIcons() == true) {
if (newButton.getTag() != null) {
newButton.setCompoundDrawablesWithIntrinsicBounds(null, ((ImageView) newButton.getTag()).getDrawable(), null, null);
newButton.setPadding(leftpadding, toppadding, rightpadding, bottompadding);
} else {
newButton.setPadding(5, 5, 5, 5);
}
} else {
newButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
newButton.setPadding(5, 5, 5, 5);
}
事实证明,当我在清单中插入指定目标和最小版本的行时,图标出现在按钮内的底部位置。
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" />
如果我只是删除线uses-sdk
,适用于大多数设备,但布局是平板电脑中的问题,例如:
ImageView 示例:
ImageView imb1 = new ImageView(this.getBaseContext());
imb1.setImageResource(R.drawable.icon_1);
imb1.setMaxHeight(120);
imb1.setMaxWidth(120);
imb1.setScaleType(ImageView.ScaleType.CENTER);
MyButton b1 = new MyButton(this);
b1.setText("Label...");
b1.setId(1001);
b1.setTag(imb1);
有人可以帮我吗?