物理和虚拟设备上的应用程序显示错误

时间:2019-08-02 19:59:10

标签: android resolution android-studio-3.4

我尝试使用具有不同布局的仿真器和物理设备,但是应用程序仅显示布局的百分之六十,例如它超出了可见屏幕,并且某些组件掉线了。

我尝试使用滚动视图,但没有成功。

<androidx.constraintlayout.widget.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"
    android:background="#242323"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="395dp"
        android:layout_height="715dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">
        <EditText
            android:id="@+id/edittext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="blah blah"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="press"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/edittext"
            />
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

3 个答案:

答案 0 :(得分:0)

在您的代码中,尝试更改此内容:
来自

 <RelativeLayout
        android:layout_width="395dp"
        android:layout_height="715dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

答案 1 :(得分:0)

使用约束布局时,您需要将视图链接到其他视图/边界。在您的情况下,您的RelativeLayout未链接到父级,并且您使用的是额外的Layout,因此请删除Constraint布局,而仅使用Relative布局。 可以。

尝试以下xml

<RelativeLayout 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:background="#242323"
    tools:context=".MainActivity">
        <EditText
            android:id="@+id/edittext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="blah blah"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="press"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/edittext"
            />
    </RelativeLayout>

答案 2 :(得分:0)

删除相对布局,仅使用约束布局。请注意,您的按钮和edittext必须限制在父视图中。在https://developer.android.com/reference/android/support/constraint/ConstraintLayout

上了解有关约束布局的更多信息

尝试以下xml

<androidx.constraintlayout.widget.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"
    android:background="#242323"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:hint="blah blah"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edittext"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="press"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edittext" />
</androidx.constraintlayout.widget.ConstraintLayout>