图像在较大的屏幕(Nexus 7)上看起来微不足道,但在较小的屏幕(Nexus 4)中填满整个屏幕

时间:2015-12-18 00:08:42

标签: android imageview

有没有办法将图像视图大小设置为显示尺寸的一部分? 我的图像应该大致占据屏幕尺寸的一半,但我无法正确设置尺寸。它在较小的屏幕(如Nexus 4)中看起来不错,但在较大的屏幕(Nexus 7)中看起来很小。我们可以通过xml完成​​吗? 或者,我是否必须在drawable-hdpi,drawable-xhdpi等中为同一图像存储不同的图像大小。我有近50张图片。存储多个副本会增加我的应用程序大小:(

1 个答案:

答案 0 :(得分:1)

  

有没有办法将图像视图大小设置为显示尺寸的一部分?

使用LinearLayoutandroid:layout_weight

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="50"
        android:text="@string/fifty_percent"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="30"
        android:text="@string/thirty_percent"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="20"
        android:text="@string/twenty_percent"/>

</LinearLayout>

(来自this sample project

或者,使用PercentFrameLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <!--suppress AndroidDomInspection -->
  <TextView
    android:id="@+id/start"
    android:layout_height="wrap_content"
    android:background="#FF00FFFF"
    android:gravity="center"
    android:padding="8dp"
    android:textColor="@android:color/black"
    app:layout_marginLeftPercent="5%"
    app:layout_widthPercent="30%"/>

  <!--suppress AndroidDomInspection -->
  <TextView
    android:id="@+id/center"
    android:layout_height="wrap_content"
    android:background="#FFFF00FF"
    android:gravity="center"
    android:padding="8dp"
    android:textColor="@android:color/black"
    app:layout_marginLeftPercent="40%"
    app:layout_widthPercent="20%"/>

  <!--suppress AndroidDomInspection -->
  <TextView
    android:id="@+id/end"
    android:layout_height="wrap_content"
    android:background="#FFFFFF00"
    android:gravity="center"
    android:padding="8dp"
    android:textColor="@android:color/black"
    app:layout_marginLeftPercent="65%"
    app:layout_widthPercent="30%"/>
</android.support.percent.PercentFrameLayout>

或者,使用PercentRelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <!--suppress AndroidDomInspection -->
  <TextView
    android:id="@+id/start"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:background="#FF00FFFF"
    android:gravity="center"
    android:padding="8dp"
    android:textColor="@android:color/black"
    app:layout_marginLeftPercent="5%"
    app:layout_widthPercent="30%"/>

  <!--suppress AndroidDomInspection -->
  <TextView
    android:id="@+id/center"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@id/start"
    android:background="#FFFF00FF"
    android:gravity="center"
    android:padding="8dp"
    android:textColor="@android:color/black"
    app:layout_marginLeftPercent="5%"
    app:layout_widthPercent="20%"/>

  <!--suppress AndroidDomInspection -->
  <TextView
    android:id="@+id/end"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@id/center"
    android:background="#FFFFFF00"
    android:gravity="center"
    android:padding="8dp"
    android:textColor="@android:color/black"
    app:layout_marginLeftPercent="5%"
    app:layout_widthPercent="30%"/>
</android.support.percent.PercentRelativeLayout>

(后两者来自this sample app

  

我是否必须在drawable-hdpi,drawable-xhdpi等中为同一图像存储不同的图像尺寸?

屏幕密度(hdpi,mdpi等)与屏幕尺寸无关。