Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓布局,元素相邻定位_Java_Android - Fatal编程技术网

Java 安卓布局,元素相邻定位

Java 安卓布局,元素相邻定位,java,android,Java,Android,我正忙着用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:orie

我正忙着用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>

但是这两个按钮显示在彼此下方,而不是彼此上方,我知道如果我将方向更改为水平,我将能够将其放置在彼此旁边,但我希望能够跨越顶部的文本视图。 我该怎么办

问候


马特

您需要使用相对布局。您将看到下面的代码不需要太多额外的属性就能实现这一点

请注意,按钮现在有三个附加属性

  • andoid:layout_below=“@id/”:将元素放置在另一个元素下
  • android:layout_alignWithParentIfMissing:如果上述元素不存在,请与父元素对齐
  • android:layout\u alignParentLeft:允许您将一个按钮放置在左侧,另一个按钮放置在右侧
更新使用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>

你能给我们看一张你想看的图片吗。