2017-05-28 91 views
-1

我剛開始使用android開發。我只需要一個帶有一些按鈕的屏幕,它可以聯繫網絡服務器,在那裏觸發一個動作,但我甚至沒有那麼遠。Android佈局xml「按鈕堆疊」(新手)

當我向佈局添加按鈕時,即使它們很好地靠在一邊,它們最終會在ontop上相互對齊,最後在ontop上創建按鈕。

此外,我改變了顏色,但它似乎並沒有被移動到模擬器中。

這是一個新鮮的設計(第二次嘗試),我不明白髮生了什麼事情。我真的不知道什麼樣的文件包括:)

我意識到這是簡單的東西,但我只是不知所措

謝謝 拉塞

Phone and design view

回答

0

你可能使用的FrameLayout,它只是將物體堆疊在一起,只支撐重力。

對於您的用例,您可以使用LinearLayout,RelativeLayout或ConstraintLayout。下面是使用的LinearLayout一個例子:

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:text="Select releases since last candy fix" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="96dp" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="16dp" 
      android:text="Button 1" /> 

     <Button 
      android:layout_width="96dp" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="16dp" 
      android:text="Button 2" /> 
    </LinearLayout> 
</LinearLayout> 

看一看不同的佈局,看看哪一個更適合您的需求,ConstraintLayout將允許您扁平化佈局,這是良好的性能。