【问题标题】:android layout in virtual device is not the same as in layout preview and real device虚拟设备中的android布局与布局预览和真实设备中的不同
【发布时间】:2019-07-01 07:20:47
【问题描述】:

环境:
Android Studio 3.4.1
activity_main.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="@string/edit_message"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        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_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:onClick="sendMessage"
        android:text="@string/button_send"
        app:layout_constraintBaseline_toBaselineOf="@+id/editText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>

在android studio布局设计选项卡中,显示如下。

我的真实手机设备配置文件:
resolution: 1440x720 px
screen size: 5.45 inch
android version: 8.1.0

在真机上正常显示

虽然在android studio虚拟设备中EditText的提示信息没有完全显示出来。
virtual device profile:
resoluton: 480x800
density: hdpi
API: 26
Target:Android 8.0(Google APIs)
Size: 4.0 inch

【问题讨论】:

  • 你能给我你的完整布局代码吗??
  • 这不是很明显吗?整个 EditText 没有足够的空间,因此是部分视图。您的虚拟设备的屏幕尺寸和密度太小。
  • @BlackBlind ,不是我添加完整的布局代码
  • @Venus 试试我的答案,它将符合您的所有要求。

标签: android android-studio android-emulator avd


【解决方案1】:

您可以尝试使用 LinearLayout 而不是 ConstraintLayout。它会轻松解决您的问题。

代码

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">


<EditText
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.75"
    android:hint="Enter a message"/>


<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.25"
    android:text="Button"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2012-04-20
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多