【问题标题】:how to make imageView take match parent of relativelayout如何使 imageView 匹配 relativelayout 的父级
【发布时间】:2017-10-24 18:59:54
【问题描述】:

我正在尝试让 ImageView 采用 ma​​tch_parentRelativeLayout 喜欢这张照片:

当我运行我的应用程序时,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>

【问题讨论】:

    标签: android android-imageview gif android-relativelayout


    【解决方案1】:

    将 ImageView 的 layout_width 更改为 match_parent 并为其添加一个新属性:android:adjustViewBounds="true"

    【讨论】:

      【解决方案2】:

      如果你想适应ImageView width 那么你必须忽略height。并且默认情况下不会拉伸以保持纵横比,因此您需要添加另一个属性来调整视图边界。所以最后你将有你的代码chages如下ImageView

       android:layout_width="match_parent"
       android:adjustViewBounds="true"
      

      调整视图边界将调整到特定高度,同时保持纵横比。

      更多

      另外,为了避免您的代码中再次出现错误,您嵌套了两个RelativeLayout,我猜这是由于背景。如果没有必要,如果不关心空间。内部RelativeLayout 视图heightwidth 应设置为match_parent!。 编码愉快!

      【讨论】:

        【解决方案3】:

        您的ImageViewlayout_widthlayout_height 设置为wrap_content 而不是match_parent

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多