小编tom*_*k28的帖子

如何将形状资源(xml)导入代码?

我一直在尝试将shape xml导入自定义View.像这样,

<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"
/>
Run Code Online (Sandbox Code Playgroud)

在我的自定义视图代码中,我尝试从外部资源中调用它

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

但如果这在模拟器上运行,它会调用错误,因为mDrawable应该是"GradidentDrawable".但是没有意义的是上层xml代码仅适用于"ShapeDrawable".我不明白为什么会这样,有人知道为什么会这样吗?

xml android

4
推荐指数
1
解决办法
4567
查看次数

标签 统计

android ×1

xml ×1