我创建了一个动态创建 50 个按钮的 android 应用程序,效果很好,但问题是我无法使用代码为这些按钮动态添加一些背景颜色,例如
buttons[i][j].setTextColor(getResources().getColor(R.color.mycolor));
buttons[i][j].setBackgroundColor(getResources().getColor(R.color.mycolor));
我的代码如下所示,谁能告诉我一些为按钮提供背景颜色的解决方案
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyMain extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mymain);
createCalender();
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[10][5];
int count=51;
int tab=1;
for (int i = 0; i<10; i++)
{
if(count%5==1)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(5);
layoutVertical.addView(rowLayout,param);
count=count-5;
}
for(int j=0;j<5;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText(""+tab);
buttons[i][j].setHeight(35);
buttons[i[j].setWidth(75);
buttons[i][j].setBackgroundResource(R.color.mycolor);
buttons[i][j].setTextColor(Color.parseColor("#FFBBFF"));
tab++;
rowLayout.addView(buttons[i][j],param);
}
}
}
}