Android – Layout not appearing as expected [duplicate]

问题内容:

This question already has an answer here:

This(1st image) is how the layout is appearing in content_main.xml
https://s8.postimg.org/8ie65tzdh/Untitled1.png
But this(2nd image) is how it appears in the virtual device.
https://s8.postimg.org/wywc0enad/Untitled.png

I’m a beginner so I might be missing many things here. All kind of help will be appreciated.

<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.shanu.namerank.MainActivity"
tools:showIn="@layout/activity_main">

<Button
    android:id="@+id/submit"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/submit"
    tools:layout_editor_absoluteX="136dp"
    tools:layout_editor_absoluteY="217dp" />

<EditText
    android:id="@+id/name"
    android:layout_width="279dp"
    android:layout_height="70dp"
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="@string/enter_name"
    tools:layout_editor_absoluteX="41dp"
    tools:layout_editor_absoluteY="130dp" />

<Button
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/check"
    tools:layout_editor_absoluteX="126dp"
    tools:layout_editor_absoluteY="284dp" />

<TextView
    android:id="@+id/result"
    android:layout_width="194dp"
    android:layout_height="67dp"
    android:text="@string/result"
    tools:layout_editor_absoluteX="83dp"
    tools:layout_editor_absoluteY="357dp" />

</android.support.constraint.ConstraintLayout>

enter image description here

问题评论:

1  
Where are the pictures?
1  
Edit your question with proper urls .
    
You forgot to add the images
    
I uploaded the links to the images.
– Shantanu
56 mins ago

答案:

答案1:

You need to add “android:layout_marginBottom= x dp”.
Bottom/Top/Start(for left)/End(for right) for all 4 sides. And for every element.

Or if you like more design side, just pull arrows from elements to the sides and then move elements over layout.

答案评论:

答案2:

You have constrains on your views that are only for showing in the xml layout editor but will be ignored when running on an actual device. Android studio should be giving you warnings regarding the usage of anything with the text “layout_editor_”. You need to give the view real constraints.

答案评论:

原文地址:

https://stackoverflow.com/questions/47755696/android-layout-not-appearing-as-expected

添加评论