set border to imageview dynamically

被刻印的时光 ゝ 提交于 2019-12-13 02:41:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!