从xml加载imageView

Ant*_*ope 4 android android-layout android-imageview

我想使用这样的命令:

ImageView image = (ImageView) findViewById(R.id.x2);
Run Code Online (Sandbox Code Playgroud)

我应将main.xml以下代码放在文件的什么位置?

<ImageView 
    android:id="@+id/x2"
    android:src="@drawable/book"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

Sol*_*ety 5

Dheeresh Singh很好。main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

   <ImageView 
          android:id="@+id/x2"
          android:src="@drawable/book"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如果要将图像放置在其他位置,则可以修改不同的属性

 <ImageView 
              android:id="@+id/x2"
              android:src="@drawable/book"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginBottom="42dp"  <!--for example  -->
              android:layout_marginLeft="25dp"
              android:layout_marginRight="25dp" 
              android:layout_marginTop="25dp"/>
Run Code Online (Sandbox Code Playgroud)

有关此的更多信息,请查看android教程

http://developer.android.com/reference/android/widget/ImageView.html(用于ImageView属性) http://developer.android.com/resources/tutorials/views/index.html(用于视图信息)

请问我的英语水平。祝好运!