如何使 imageView 匹配 relativelayout 的父级
how to make imageView take match parent of relativelayout
我正在尝试让 ImageView
获得 match_parent 的 RelativeLayout
喜欢这张照片:
当我 运行 我的应用程序 ImageView
看起来像这样时 :
这是我的 xml :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linealL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/khdar">
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:id="@+id/RelativeLayout02"
android:layout_width="wrap_content"
android:background="@color/colorGriviewBorder"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_margin="5dp"
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gradient"
android:orientation="vertical" >
<TextView
android:text="GIF"
android:textSize="16sp"
android:layout_gravity="center"
android:textColor="@color/colorGriviewBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dev"/>
</LinearLayout>
<ImageView
android:padding="8dp"
android:layout_below="@+id/LinearLayout01"
android:contentDescription="@string/app_name"
android:id="@+id/imageSelectTo"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
您的 ImageView
的 layout_width
和 layout_height
设置为 wrap_content
而不是 match_parent
。
将 ImageView 的 layout_width
更改为 match_parent
并为其添加一个新属性:android:adjustViewBounds="true"
.
如果你想适应 ImageView
width
那么你必须忽略 height
。并且默认情况下不会为您保持宽高比而拉伸,因此您需要添加另一个属性来调整视图边界。所以最后你将有你的代码 chages 如下 ImageView
:
android:layout_width="match_parent"
android:adjustViewBounds="true"
调整视图边界将调整到特定高度,同时保持纵横比。
更多
另外,为了避免在您的代码中再次出现错误,您嵌套了两个 RelativeLayout
,我猜这是由于背景。如果没有那个必要的话不关心space。内部 RelativeLayout
视图 height
和 width
应设置为 match_parent
!。 编码愉快!
我正在尝试让 ImageView
获得 match_parent 的 RelativeLayout
喜欢这张照片:
当我 运行 我的应用程序 ImageView
看起来像这样时 :
这是我的 xml :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linealL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/khdar">
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:id="@+id/RelativeLayout02"
android:layout_width="wrap_content"
android:background="@color/colorGriviewBorder"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_margin="5dp"
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gradient"
android:orientation="vertical" >
<TextView
android:text="GIF"
android:textSize="16sp"
android:layout_gravity="center"
android:textColor="@color/colorGriviewBorder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dev"/>
</LinearLayout>
<ImageView
android:padding="8dp"
android:layout_below="@+id/LinearLayout01"
android:contentDescription="@string/app_name"
android:id="@+id/imageSelectTo"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
您的 ImageView
的 layout_width
和 layout_height
设置为 wrap_content
而不是 match_parent
。
将 ImageView 的 layout_width
更改为 match_parent
并为其添加一个新属性:android:adjustViewBounds="true"
.
如果你想适应 ImageView
width
那么你必须忽略 height
。并且默认情况下不会为您保持宽高比而拉伸,因此您需要添加另一个属性来调整视图边界。所以最后你将有你的代码 chages 如下 ImageView
:
android:layout_width="match_parent"
android:adjustViewBounds="true"
调整视图边界将调整到特定高度,同时保持纵横比。
更多
另外,为了避免在您的代码中再次出现错误,您嵌套了两个 RelativeLayout
,我猜这是由于背景。如果没有那个必要的话不关心space。内部 RelativeLayout
视图 height
和 width
应设置为 match_parent
!。 编码愉快!