java - 约束布局不适合不同设备

标签 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

最佳答案

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

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

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

关于java - 约束布局不适合不同设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44486396/

相关文章:

java - jboss:通过java代码访问文件资源?

java - Gridworld 案例研究 (java) : Mac Errors

android XML scrollview 和线性布局问题

java - 在 Android 中映射空间数据

android - Android 广播接收器的 IOS 等效项

xml - 默认声明了哪些 XML 命名空间?

java - 获取xml中所有id属性的值,看看是否有重复

java - 拆分字符串、整数和特殊字符 ("_")

java - 使用java连接mysql数据库出错

php - 如何将 XML 数据的元素转换为 PHP 中的单个关联数组?