【问题标题】:Imageview images responsivenessImageview 图像响应能力
【发布时间】:2018-02-19 12:52:37
【问题描述】:

我有 3 张 PNG 格式的图像。它们具有相同的分辨率,我希望它们在相对布局中彼此相邻。我希望它具有响应性,例如图像视图应根据提供的空间调整它们的大小。我不想在 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>

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

【问题讨论】:

  • 使用一种水平线性布局。将 3 个图像视图放入其中。在每个图像视图中将权重添加到 1

标签: android android-imageview android-relativelayout


【解决方案1】:

您可以通过使用属性 android:layout_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】:
    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      相关资源
      最近更新 更多