在其他元素之间放置一个图像,以便它可以缩放以适应所有屏幕,同时保持纵横比不变

时间:2017-04-12 15:54:44

标签: android android-layout

我想在我的xml中这样做。目前我正在这样做:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.MainActivity">
    <ImageView
        android:id="@+id/main_drawable_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/text" />
    <LinearLayout
        android:id="@+id/main_drawable_logo"
        android:layout_width="match_parent"
        android:layout_below="@id/main_drawable_text"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.1"
            android:layout_centerHorizontal="true"
            android:adjustViewBounds="true"
            android:src="@drawable/logo" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_below="@id/main_drawable_logo"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/credits"
            android:layout_width="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
            android:text="@string/credits" />
    </LinearLayout>
</RelativeLayout>

在我的测试设备中,我只获得了@drawable/logo的一部分,剩下的就像信用证的文本一样掉在了屏幕之外。我当然可以调整重量,直到我得到所需的结果,但我怎样才能确保我会在所有设备上得到预期的结果?

我想要的是main_drawable_text总是匹配屏幕的宽度(从边距到边距),credits始终位于屏幕的底部,drawable_logo已缩放以适合它们之间的自由空间,但具有原始的宽高比。

1 个答案:

答案 0 :(得分:1)

如果我完全理解您要设计的用户界面,则不需要LinearLayout但只需要现有的RelativeLayout,并且在其中您可以根据其他位置放置您的元素(使用android:layout_aboveandroid:layout_below等等。保持可绘制使用的原始比率android:scaleType="fitCenter"

这是一个显示(我的解释)你想要的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <ImageView
        android:id="@+id/main_drawable_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/text"/>

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_above="@id/credits"
        android:layout_below="@id/main_drawable_text"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/logo"/>

    <TextView
        android:id="@+id/credits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/credits"/>

</RelativeLayout>