android button setbackgroundcolor,有没有办法改变按钮后面的背景颜色(button.setBackgroundResource)...

我是一名新的Android开发学生,我正在尝试创建一个包含TextView和同一行内按钮的动态布局。

但我有一点问题。我在我的Drawables资源中设置了我的按钮

button.setBackgroundResource(R.drawable.ic_comment_black_48px);

现在,我无法改变它背后的背景颜色。

我在主要的LinearLayout中创建了一个newLinearlayout,并创建了一个新的textView和一个新的Button。我已将它们放在LinearLayout的子项中并将其放在main中。这是工作,但不是我的按钮背后的背景颜色。

有没有办法做到这一点?

我的xml布局

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="@color/grey"

android:id="@+id/historyLayout">

我的完整活动

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_history);

mLinearLayout = findViewById(R.id.historyLayout);

mMoodSaved = new ArrayList(7); // Define the max size of my ArrayList

loadData();

for (int i = 1; i <= 7; i++) {

final TextView textView = new TextView(this);

mLinearLyt = new LinearLayout(this);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

mLinearLyt.setOrientation(LinearLayout.HORIZONTAL);

textView.setHeight(300);

textView.setWidth(400);

textView.setBackgroundColor(Color.RED);

textView.setTextColor(Color.BLACK);

textView.setTextSize(12);

textView.setText(String.valueOf(i));

mLinearLyt.setBackgroundColor(Color.YELLOW);

mLinearLyt.addView(textView);

mLinearLyt.setLayoutParams(params);

ImageButton button = new ImageButton(this);

LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

param2.setMargins(20,50,0,0);

param2.height = 100;

param2.width = 100;

button.setLayoutParams(param2);

button.setBackgroundResource(R.drawable.ic_comment_black_48px);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

soundConfirm();

Toast.makeText(getApplicationContext(), textView.getText(), Toast.LENGTH_LONG).show(); //Display Toast Message

}

});

mLinearLyt.addView(button);

mLinearLayout.addView(mLinearLyt);

}

}

0

投票

由于这是一个ImageButton,请设置

button.setImageResource(R.drawable.ic_comment_black_48px)

而不是setBackgroundResource。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值