首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android应用布局问题:元素出现在手机屏幕的角落

Android应用布局问题:元素出现在手机屏幕的角落
EN

Stack Overflow用户
提问于 2017-08-08 20:15:21
回答 4查看 660关注 0票数 0

我对Android应用程序开发非常陌生。我使用拖放元素的方式设计了布局。但当我在移动设备上运行这个应用程序时,该应用程序的所有元素都聚集在显示屏的左上角。我添加了如何设计布局的屏幕截图

以及它在我的手机屏幕上的显示方式。

下面是XML代码:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jyotirmoy.myapplication.MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Enter any text"
        tools:layout_editor_absoluteX="70dp"
        tools:layout_editor_absoluteY="57dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Print"
        tools:layout_editor_absoluteX="89dp"
        tools:layout_editor_absoluteY="-250dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your text will be displayed here:"
        tools:layout_editor_absoluteX="307dp"
        tools:layout_editor_absoluteY="-35dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onButtonClick"
        android:text="Print"
        tools:layout_editor_absoluteX="133dp"
        tools:layout_editor_absoluteY="122dp" />
</android.support.constraint.ConstraintLayout>
EN

回答 4

Stack Overflow用户

发布于 2017-08-08 20:23:04

这里的问题是,在使用ConstraintLayout时,您没有为视图使用约束。您应该为视图设置约束,使其显示为您设计的正确布局。

代码语言:javascript
运行
复制
<?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">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="44dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Enter any text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:onClick="onButtonClick"
        android:text="Print"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>
票数 4
EN

Stack Overflow用户

发布于 2017-08-08 20:18:13

LinearLayout中使用整个布局;

代码语言:javascript
运行
复制
 <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Enter any text"
    tools:layout_editor_absoluteX="70dp"
    tools:layout_editor_absoluteY="57dp" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Print"
    tools:layout_editor_absoluteX="89dp"
    tools:layout_editor_absoluteY="-250dp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your text will be displayed here:"
    tools:layout_editor_absoluteX="307dp"
    tools:layout_editor_absoluteY="-35dp" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onButtonClick"
    android:text="Print"
    tools:layout_editor_absoluteX="133dp"
    tools:layout_editor_absoluteY="122dp" />

</LinearLayout>
票数 0
EN

Stack Overflow用户

发布于 2017-08-08 20:19:21

您可以使用垂直方向的LinearLayout而不是ConstraintLayout

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45568099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档