Android 如何将可绘制(.xml)动态添加到按钮(动态生成)
我有一个方法可以动态创建一个Android 如何将可绘制(.xml)动态添加到按钮(动态生成),android,android-layout,button,Android,Android Layout,Button,我有一个方法可以动态创建一个按钮数组 for(int i = 0; i < 10; i++){ Button button = new Button(activity); LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(200, 100); button.setLayoutParams(lparams); button.setTag(0); buttonA
按钮数组
for(int i = 0; i < 10; i++){
Button button = new Button(activity);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(200, 100);
button.setLayoutParams(lparams);
button.setTag(0);
buttonArray[i] = button;
layout.addView(button);
}
我不能做的是将d.xml应用于我动态创建的按钮
有人能帮我吗?提前感谢。使用按钮的设置后台资源:
for(int i = 0; i < 10; i++){
Button button = new Button(activity);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(200, 100);
button.setLayoutParams(lparams);
button.setTag(0);
button.setBackgroundResource(R.drawable.d);
buttonArray[i] = button;
layout.addView(button);
}
for(int i=0;i<10;i++){
按钮按钮=新按钮(活动);
LinearLayout.LayoutParams lparams=新的LinearLayout.LayoutParams(200100);
按钮。setLayoutParams(LPARAM);
按钮。设置标签(0);
button.setBackgroundResource(R.drawable.d);
buttonArray[i]=按钮;
布局。添加视图(按钮);
}
使用按钮的设置背景资源:
for(int i = 0; i < 10; i++){
Button button = new Button(activity);
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(200, 100);
button.setLayoutParams(lparams);
button.setTag(0);
button.setBackgroundResource(R.drawable.d);
buttonArray[i] = button;
layout.addView(button);
}
for(int i=0;i<10;i++){
按钮按钮=新按钮(活动);
LinearLayout.LayoutParams lparams=新的LinearLayout.LayoutParams(200100);
按钮。setLayoutParams(LPARAM);
按钮。设置标签(0);
button.setBackgroundResource(R.drawable.d);
buttonArray[i]=按钮;
布局。添加视图(按钮);
}
使用
使用
在for循环中,在
按钮.setTag(0)之后代码>执行此操作按钮.setBackground(ContextCompat.getDrawable(this,R.drawable.d))代码>在for循环中,在按钮之后。setTag(0)代码>执行此操作按钮.setBackground(ContextCompat.getDrawable(this,R.drawable.d))代码>一旦创建了后台可绘制文件,就可以将其应用于代码中动态生成的按钮
如果你的最小构建是API21(棒棒糖),那么使用
button.setBackground(activity.getDrawable(R.drawable.d)
对于较低的API,您还可以使用setBackgroundDrawable
可以使用更合适的解决方案
button.setBackgroundResouce(R.drawable.d)
试试看 一旦创建了后台绘图,就可以将其应用于代码中动态生成的按钮
如果你的最小构建是API21(棒棒糖),那么使用
button.setBackground(activity.getDrawable(R.drawable.d)
对于较低的API,您还可以使用setBackgroundDrawable
可以使用更合适的解决方案
button.setBackgroundResouce(R.drawable.d)
试试看