簡體   English   中英

Android布局,彼此相鄰放置元素

[英]Android Layout, positioning elements next to eachother

我正在忙於在android中設計我的第一個應用程序,但遇到了一些問題。

我創建了這個:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.mfr.calculatorapp.MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:layout_margin="15dp"
    android:text="123"
    android:id="@+id/textView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/number_button_size"
    android:textSize="@dimen/number_button_size"
    android:layout_margin="10dp"
    android:text="1"
    android:id="@+id/button_1" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/number_button_size"
    android:textSize="@dimen/number_button_size"
    android:layout_margin="10dp"
    android:text="1"
    android:id="@+id/button_1" />


</LinearLayout>

但是這兩個按鈕顯示在彼此的下方,而不是在彼此的上方,我知道,如果將方向更改為水平,則可以將它們彼此相鄰放置,但是我希望能夠將textview覆蓋在頂部。 我該怎么辦?

問候

馬特

您將要使用相對布局。 您將看到以下代碼不需要很多額外的屬性即可實現此目的。

請注意,按鈕現在具有三個附加屬性。

  • andoid:layout_below =“ @ id /”:將元素放置在另一個元素下
  • android:layout_alignWithParentIfMissing:如果上述元素不存在,請與父元素對齊
  • android:layout_alignParentLeft:允許您將一個按鈕放置在左側,將一個按鈕放置在右側

UPDATE使用android:layout_alignRight =“ @ id / textView”將第二個按鈕正確地向右對齊

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.mfr.calculatorapp.MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:layout_margin="15dp"
    android:text="123"
    android:id="@+id/textView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/number_button_size"
    android:textSize="@dimen/number_button_size"
    android:layout_margin="10dp"
    android:text="1"
    android:id="@+id/button_1"  
    android:layout_below="@id/textView"
    android:layout_alignWithParentIfMissing="true"
    android:alignParentLeft="true"
/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/number_button_size"
    android:textSize="@dimen/number_button_size"
    android:layout_margin="10dp"
    android:text="2"
    android:id="@+id/button_2" 
    android:layout_below="@id/textView"
    android:layout_alignWithParentIfMissing="true"
    android:alignParentRight="true"
    android:layout_alignRight="@id/textView"
/>

</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM