Android xml属性以编程方式

时间:2017-08-01 14:20:08

标签: java android xml

我正在改变我的按钮样式:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)btBroadcast.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
btBroadcast.setLayoutParams(params);

如何在上面添加这四条规则?

android:layout_marginRight="16dp"
android:scaleType="centerInside"
android:layout_width="20"
android:layout_height="20"

1 个答案:

答案 0 :(得分:1)

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)btBroadcast.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.height = 10;
params.width = 20;
params.rightMargin = 10;
btBroadcast.setScaleType(ImageView.ScaleType.CENTER);
btBroadcast.setBackgroundResource(R.drawable.image);
btBroadcast.requestLayout();