代码之家  ›  专栏  ›  技术社区  ›  Kylo Ren

Imageview图像响应

  •  0
  • Kylo Ren  · 技术社区  · 7 年前

    我有3张PNG格式的图片。它们的分辨率相同,我希望它们在相对布局中彼此相邻。我希望它能够响应,例如,ImageView应该根据提供的空间调整其大小。我不想在dps中给出固定的高度和宽度,例如:

    <ImageView
                android:layout_width="60dp"
                android:layout_height="100dp"/>
    

    如果我试图定义宽度和高度,如:

    <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    

    如果我这样做了,它们就太大了。我该怎么办?有什么建议吗?

    编辑: 代码如下:

    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/streetlong">
            <ImageView
                android:layout_width="60dp"
                android:layout_height="100dp"
                android:id="@+id/btn_hospital"
                android:onClick="onClick"
                android:background="@drawable/hospital"
                />
    
            <ImageView
                android:id="@+id/btn_pharmacy"
                android:layout_width="60dp"
                android:layout_height="100dp"
                android:layout_marginLeft="@dimen/_15sdp"
                android:onClick="onClick"
                android:layout_toRightOf="@+id/btn_hospital"
                android:layout_toEndOf="@+id/btn_hospital"
                android:background="@drawable/pharmacys" />
            <ImageView
                android:id="@+id/btn_police"
                android:layout_width="60dp"
                android:layout_height="100dp"
                android:layout_marginLeft="@dimen/_15sdp"
                android:layout_toRightOf="@+id/btn_pharmacy"
                android:layout_toEndOf="@+id/btn_pharmacy"
                android:layout_weight="1"
                android:background="@drawable/polices"
                android:onClick="onClick" />
        </RelativeLayout>
    

    我希望图像视图根据屏幕大小进行调整,我不想给它们高度和宽度

    2 回复  |  直到 7 年前
        1
  •  2
  •   Bunny    5 年前

    您可以通过使用属性android:layout\u weight来实现这一点

      <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3>
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
    
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:layout_weight="1"/>
    
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>
        </RelativeLayout>
    
        2
  •  2
  •   Radesh    7 年前
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        >
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    
    </LinearLayout>