2017-04-08 76 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> 
相關問題