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