首页 > 解决方案 > LinearLayout 中的自动缩放元素

问题描述

我有一个简单的片段,我希望它看起来像这个图像:image

我的片段代码:

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


    <android.support.constraint.ConstraintLayout
        android:id="@+id/wrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/wrapper_padding">

        <ImageView
            android:id="@+id/imageOne"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@drawable/example" />
    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp">

        <TextView
            android:id="@+id/infoMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/info_message_example"
            android:textColor="@color/colorInfoMessage"
            android:textSize="25sp" />

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

但我的片段看起来像这样的图像:image。在景观变体上,文本被隐藏。

我尝试在<LinearLayout/>或其他元素上添加一些属性。当我在第一个 ConstraintLayout 上添加 android:layout_weight="1" 时,我得到了这个

横向变化是正确的,但纵向方向被破坏了。

如何更改我的 xml 文件以获得正确的结果?

标签: android

解决方案


要为纵向和横向模式创建单独的片段设计,请创建两个不同的XML文件,一个用于纵向模式设计,另一个用于横向模式。放入layout-portlayout-land文件夹里面res folder

例子:

res/layout-port [Portrait Mode; default]
main.xml
res/layout-land [Landscape Mode]
main1.xml 

参考文档:http: //developer.android.com/training/basics/supporting-devices/screens.html


推荐阅读