5

我想使用这样的命令:

ImageView image = (ImageView) findViewById(R.id.x2);

main.xml我应该将以下代码放在文件中的哪个位置?

<ImageView 
    android:id="@+id/x2"
    android:src="@drawable/book"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
4

3 回答 3

6

Dheeresh Singh 没问题。主.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>

如果想把图片定位在不同的地方,可以修改不同的属性

 <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"/>

有关这方面的更多信息,请查看 android 教程

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

对不起我的英语。祝你好运!

于 2012-06-12T13:11:35.473 回答
0

在您设置的活动的布局 xml 中设置setContentView(r.layout.<id>)

请参考教程设置setContentView

Android 初学者指南

于 2012-06-12T12:58:22.023 回答
0

XML Layouts开发人员指南中所述,您应该main.xml在 /res/layout/ 下创建您的并放入如下可用布局ImageView之一:

<?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" 
          android:orientation="vertical" >

<ImageView 
      android:id="@+id/x2"
      android:src="@drawable/book"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

</LinearLayout>
于 2012-06-12T12:59:47.597 回答