约束布局不适合不同的设备

时间:2017-06-11 16:55:54

标签: java android xml android-layout constraints

我正在尝试使用nexus5调整我使用的布局,但它在我的设备上总是有不同的尺寸,我不知道为什么。

我使用约束布局来构建它,并且我知道约束应该适应不同的设备但它不起作用;我总是将自己的内容变得更大或与其他内容重叠。

这是我正在构建的布局的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.afcosta.inesctec.pt.android.Login"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="25dp">

    <android.support.design.widget.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="52dp"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toTopOf="@+id/textInputLayout6"
        android:layout_marginStart="45dp"
        android:layout_marginEnd="45dp"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="33dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:id="@+id/textInputLayout7">

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="username" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:id="@+id/textInputLayout6"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toTopOf="@+id/Login"
        android:layout_marginStart="43dp"
        android:layout_marginEnd="43dp"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="42dp"
        app:layout_constraintLeft_toLeftOf="parent">

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="password"
            android:inputType="textPassword" />
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/Login"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="@color/emerald"
        android:text="Entrar"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="@+id/textInputLayout6"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="143dp"
        app:layout_constraintLeft_toLeftOf="@+id/textInputLayout6" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ainda não tem conta?"
        android:layout_marginStart="26dp"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView5"
        tools:layout_constraintBaseline_creator="1"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="@+id/Login" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Registe-se"
        android:textColor="@color/nephritis"
        tools:layout_constraintTop_creator="1"
        android:layout_marginStart="13dp"
        android:layout_marginTop="20dp"
        app:layout_constraintTop_toBottomOf="@+id/Login"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toRightOf="@+id/textView3" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="232dp"
        android:layout_height="230dp"
        app:srcCompat="@drawable/flora"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/textInputLayout7" />
</android.support.constraint.ConstraintLayout>

这就是我在模拟器上看到的内容: layout

这就是我在设备上看到的内容: layout

2 个答案:

答案 0 :(得分:0)

•在布局XML文件中使用与分辨率无关的单位

•使用布局管理器声明布局而不参考绝对屏幕位置

•使用多个资源文件夹为不同的屏幕尺寸提供不同的布局

答案 1 :(得分:0)

从Android指南https://developer.android.com/training/multiscreen/screensizes.html

引用

&#34;为了确保您的布局灵活并适应不同的屏幕尺寸,您应该使用&#34; wrap_content&#34;和&#34; match_parent&#34;对于某些视图组件的宽度和高度&#34; 我可以看到您的图像具有硬代码高度,宽度,边距可能是这种情况

尝试使用<RelativeLayout>代替