How to import form resource (xml) into code?

I am trying to import an xml form into a custom view. eg,

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid 
  android:color="#f0600000"/>
<stroke
  android:width="10dp" 
  android:color="#00FF00"/>
<corners 
  android:radius="15dp" />
<padding 
  android:left="10dp"
  android:top="10dp"
  android:right="10dp"
  android:bottom="10dp"
/>

and in my customized view code I try to call it from external resources

    private void initTestView(){
    Resources res = this.getResources();
    mDrawable = (ShapeDrawable)res.getDrawable(R.drawable.recshape);
}

but if it runs on an emulator, it is called an error because mDrawable must be "GradidentDrawable". but the point is not that the top xml code is only for "ShapeDrawable". I don’t understand why this is happening, does anyone know why this is happening?

+3
source share
1 answer

, <shape> XML- GradientDrawable, ShapeDrawable . , Drawable, .

, Nine Patch , , .

+11

Source: https://habr.com/ru/post/1750613/


All Articles