简体   繁体   English

RelativeLayout中的ImageView不遵循其父级高度

[英]ImageView inside RelativeLayout not obbeing its parent height

I have the following code in create_button.xml : 我在create_button.xml中有以下代码:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="60dp" 
    android:layout_width="wrap_content"
    android:padding="3dp"
    android:background="@color/green">
      <ImageView
          android:id="@+id/test_button_image"
          android:layout_width="wrap_content"
          android:src="@drawable/backImage"
          android:contentDescription="@string/app_name"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true">
       </ImageView>
</RelativeLayout>

This piece of code is included as a fragment of an application as follows: 这段代码作为应用程序的一部分包含在内,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:baselineAligned="false"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

    <include android:id="@+id/buttonCreate"
       layout="@layout/create_button"
       android:layout_weight="1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content">
    </include>
</LinearLayout>

But my image is not obbeing to the RelativeLayout layout_height of 60dp and it's expandig to its full original height. 但是我的图像不符合60dp的RelativeLayout layout_height的要求,并且会展开到其原始高度。

I tried to change its layout_height to match_parent , with no luck. 我试图将其layout_height更改为match_parent ,但是没有运气。 It expands full screen height. 它扩展了全屏高度。

How can I make this image to be the same height as its parent RelativeLayout ? 如何使此图像与其父RelativeLayout高度相同?

I think following code will work for you. 我认为以下代码将为您工作。

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" 
android:layout_width="wrap_content"
android:padding="3dp"
android:background="@color/green">
  <ImageView
      android:id="@+id/test_button_image"
      android:layout_width="wrap_content"
      android:src="@drawable/backImage"
      android:contentDescription="@string/app_name"
      android:layout_height="60dp"
      android:layout_alignParentTop="true">
   </ImageView></RelativeLayout>

Exchange the height of Relative layout with ImageView and vice versa. 用ImageView交换相对布局的高度,反之亦然。

Try this. 尝试这个。 It will never expandig to its full original height. 它永远不会扩展到其原始高度。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:background="@color/green"
android:padding="3dp" >

<ImageView
    android:id="@+id/test_button_image"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/backImage" >
</ImageView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM