2017-08-08 35 views
0

我对Android应用程序开发非常陌生。我使用拖放元素设计了布局。但是,当我在移动设备上运行此应用程序时,应用程序中的所有元素都聚集在显示屏的左上角。我已经添加截图我如何设计布局Android应用程序布局问题:元素出现在手机屏幕的角落

how I designed

以及它是如何出现在我的手机屏幕上。

how it appears

这里的XML代码:

<?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> 

回答

0

使用内部LinearLayout整个布局;

<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> 
+2

此外,它破坏了'ConstraintLayout'的目的 – reVerse

0

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

4

使用ConstraintLayout你应该把约束你的意见,以显示为您设计的时候,这里你不使用你的意见约束的问题 正确的布局就会像有

<?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> 
0

如果布局是这样简单那么就没有必要使用ConstraintLayout。检查出LinearLayout您可以在其中设置项目的方向。或RelativeLayout您可以在其中获得更多的物品灵活性。

同时在这里与LinearLayout一个解决方案:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center"> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="Enter any text"/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="print"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Your text will appear here"/> 
</LinearLayout>