Shape Drawable不会在ImageView中显示

时间:2015-06-29 19:25:31

标签: android xml shapedrawable

我正在尝试在Android应用中制作矩形块。我尝试使用Shape Drawable但它不起作用。 rectangle.xml:

<?xml version="1.0" encoding="utf-8"?><shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
    android:width="1dp"
    android:color="#000000">
</stroke>
<solid android:color="#f0600000"/>
<size android:height="30dp" android:width="30dp"/></shape>

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="200dp"
    android:layout_height="200dp" android:background="@android:color/holo_red_dark"
    android:layout_centerInParent="true"
    android:scaleType="center"
    android:src="@drawable/rectangle">

</ImageView></RelativeLayout>

MainActivity.java ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.imageView);
    setDrawable();

}

private void  setDrawable(){
    imageView.setImageResource(R.drawable.rectangle);
}

我在模拟器中看到的只是方形,没有实心笔画。为什么不显示可绘制的资源?感谢

1 个答案:

答案 0 :(得分:0)

试试这个:

imageView.setImageDrawable(getResources().getDrawable(R.drawable.rectangle));