有没有办法将图像视图大小设置为显示大小的比例?我有一张大约占屏幕大小一半的图像,但我无法正确设置尺寸。它在较小的屏幕(如Nexus 4)中看起来不错,但在较大的屏幕(Nexus 7)中看起来很小。我们可以通过xml实现吗?或者,我是否必须将同一图像的不同图像大小存储在可绘制的hdpi、可绘制的xhdpi等中。??我有将近50张照片。存储多个副本会大大增加我的应用程序大小:(
有没有办法将图像视图大小设置为显示大小的比例?
使用LinearLayout
和android: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>
(来自此示例项目)
或者,使用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>
(后两个来自这个示例应用程序)
我是否必须将同一图像的不同图像大小存储在可绘制的hdpi、可绘制的xhdpi等中。?
屏幕密度(hdpi、mdpi等)与屏幕size无关。