简体   繁体   English

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

[英]Android Layout, positioning elements next to eachother

I am busy designing my first app in android but I am having some problems. 我正在忙于在android中设计我的第一个应用程序,但遇到了一些问题。

I have created this: 我创建了这个:

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

But the two Buttons appear underneath each other and not on top of each other, I know that if I change orientation to horizontal that I will be able to position it next to each other but I want to be able to span a textview over the top. 但是这两个按钮显示在彼此的下方,而不是在彼此的上方,我知道,如果将方向更改为水平,则可以将它们彼此相邻放置,但是我希望能够将textview覆盖在顶部。 What do I do? 我该怎么办?

Regards 问候

Matt 马特

You will want to use a relative layout. 您将要使用相对布局。 You will see the below code didn't need a lot of extra attributes to pull this off. 您将看到以下代码不需要很多额外的属性即可实现此目的。

Notice that the buttons now have three additional attributes. 请注意,按钮现在具有三个附加属性。

  • andoid:layout_below="@id/" : positions the element under another element andoid:layout_below =“ @ id /”:将元素放置在另一个元素下
  • android:layout_alignWithParentIfMissing : if the above mentioned element is not present, align with the parent android:layout_alignWithParentIfMissing:如果上述元素不存在,请与父元素对齐
  • android:layout_alignParentLeft : allows you to position one button to the left and one to the right android:layout_alignParentLeft:允许您将一个按钮放置在左侧,将一个按钮放置在右侧

UPDATE use android:layout_alignRight="@id/textView" to properly align your second button to the right 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