2017-04-08 121 views
-1

xml layout in android studio 你能帮我在android xml吗?

<Button 
     android:id="@+id/bintent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Activity" 
     tools:layout_editor_absoluteY="132dp" 
     tools:layout_editor_absoluteX="136dp" /> 

    <Button 
     android:id="@+id/balram1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Alarm" 
     android:layout_centerInParent="true" 
     tools:layout_editor_absoluteY="231dp" 
     tools:layout_editor_absoluteX="136dp" /> 

` but it appears in actual device like this image

所以我应该怎么做才能显示它的实际设备

+0

你可以粘贴完整的XML? – fightingCoder

+0

该属性仅用于编辑器。 **工具:属性从代码中剥离,仅用于开发目的**。正如我上面看到的,它将绝对显示'Button'的位置 –

回答

0

这几乎是不可能告诉这是怎么回事不完整的XML文件上。我假设父布局是一个RelativeLayout,因为你使用的是centerInParent属性,但我不确定你为什么使用绝对的X和Y值。他们甚至不会做任何事情,因为“工具”命名空间仅适用于布局编辑器。它根本不影响应用程序。

我最好的猜测是你的父母布局的宽度和高度设置为“wrap_content”而不是“match_parent”,否则你的闹钟按钮将在屏幕上居中显示。

0

您在这里遇到的问题是您使用了tools:layout_editor_absoluteXtools:layout_editor_absoluteY属性。以tools开头的属性仅适用于布局设计器。

你应该怎么做,如果你想中心的按钮,是使用相对布局作为父元素和内部它可以把你的按钮。因此,它应该是这样的:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:layout_below="@+id/button2" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="36dp"/> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:layout_alignParentTop="true" 
     android:layout_alignStart="@+id/button3" 
     android:layout_marginTop="42dp"/> 
</RelativeLayout> 
0

像所有其他的答案,我也不能确定您所使用的父布局,但地方在我心目中是约束布局。如果是,则使用下面的代码:)

<?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:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/bintent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Activity" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="150dp" /> 

    <Button 
     android:id="@+id/balram1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Alarm" 
     android:layout_centerInParent="true" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginTop="61dp" 
     app:layout_constraintTop_toBottomOf="@+id/bintent" /> 
</android.support.constraint.ConstraintLayout>