Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何检测图像的宽度和高度?_Android_Xml_Imageview - Fatal编程技术网

Android 如何检测图像的宽度和高度?

Android 如何检测图像的宽度和高度?,android,xml,imageview,Android,Xml,Imageview,我有这个ImageView,它占据了整个页面。图像本身约为图像的1/3,居中于图像视图的中间 如何检测图像的宽度和高度,以便ImageView能够环绕图像而不是填满整个页面 代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent

我有这个ImageView,它占据了整个页面。图像本身约为图像的1/3,居中于图像视图的中间

如何检测图像的宽度和高度,以便ImageView能够环绕图像而不是填满整个页面

代码:

<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=".MainActivity" >

<ImageView
    android:id="@+id/ivimage"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:src="@drawable/picture" />


将android:layout\u height=“fill\u parent”切换为android:layout\u height=“wrap\u content”

填充父项(API级别8及更高版本中已弃用并重命名的匹配父项)

将小部件的布局设置为填充父项将迫使其展开,以在其所在的布局元素中占据尽可能多的可用空间

如果您知道图像的大小,还可以通过编程设置ImageView的大小

image_view.getLayoutParams().height = 20;

(注意)如果您在布局“布局”后设置高度,请确保调用image_view.requestLayout()

您不需要,只需设置
android:adjustViewBounds=“true”
@jyoon即可,谢谢