问题
I have a xml file in drawable to set border for imageview know as imgborder.xml as below
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
May I know how can I apply it dynamically in code? Like android:background in xml layout?
Thank you.
回答1:
You can do it like the following:
ImageView v = new ImageView(this);
v.setBackgroundResource(R.drawable.btn_default);
You can also use v.setBackgroundDrawable if you have a Drawable object instead of the drawable's resource id.
来源:https://stackoverflow.com/questions/7084930/set-border-to-imageview-dynamically