Android 从 xml 加载 imageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10997063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
load an imageView from xml
提问by Antilope
I want to use a command like this:
我想使用这样的命令:
ImageView image = (ImageView) findViewById(R.id.x2);
where should I put in the file main.xml
the following piece of code?
我应该将main.xml
以下代码放在文件中的什么位置?
<ImageView
android:id="@+id/x2"
android:src="@drawable/book"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
回答by SolArabehety
Dheeresh Singh is ok. main.xml:
Dheeresh Singh 还行。主文件:
<?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>
If you want to position the image in a different place, you can modify different attributes
如果想把图片定位在不同的地方,可以修改不同的属性
<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"/>
For more information about this, look at the android tutorials
有关这方面的更多信息,请查看 android 教程
http://developer.android.com/reference/android/widget/ImageView.html(for ImageView attributes) http://developer.android.com/resources/tutorials/views/index.html(for view info)
http://developer.android.com/reference/android/widget/ImageView.html(用于 ImageView 属性) http://developer.android.com/resources/tutorials/views/index.html(用于查看信息)
Excuse me for my english. Good Luck!
请原谅我的英语。祝你好运!
回答by Dheeresh Singh
Set in your layout xml of the activity which you set in setContentView(r.layout.<id>)
在您设置的活动的布局 xml 中设置 setContentView(r.layout.<id>)
please refer tutorial to set setContentView
请参考教程设置 setContentView
回答by Andrey Ermakov
As described in XML Layoutsdeveloper guide you should create your main.xml
under /res/layout/ and put ImageView
inside one of available layoutslike that:
如XML 布局开发人员指南中所述,您应该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>