我一直在尝试在我的活动中显示PNG文件,但是没有成功。
我有一个名为arrow.png
的文件在我的res/drawable
文件夹中,我的代码如下:
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow" />
</RelativeLayout>
Java:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imgView = (ImageView) findViewById(R.id.image);
imgView.setImageResource(R.drawable.arrow);
}
}
在我阅读的所有指南和示例中,它们都给了我与上面创建的相同的指令。但是当我运行这个程序时,没有任何东西显示出来(TextView 显示正常 - 图像不存在)。我对此感到非常困惑,因为没有错误消息。
我已经检查过 PNG 文件是否损坏,但也不是这种情况。我已经清理了项目并重新启动,但这仍然没有帮助。我缺少了什么/忽略了什么?
android:src
中,所以应该也会显示在那里。 - Ivo