Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将可绘制(.xml)动态添加到按钮(动态生成)_Android_Android Layout_Button - Fatal编程技术网

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)
试试看