[英]how can i position a button below two linear layouts(nested views) containted in a linear layout (root view)
我知道我可以使用相對布局,但它看起來有點復雜,視圖之間有很多交互。 我知道我也可以使用約束布局,但現在我還不知道如何使用它。
我的問題是:
有沒有辦法將按鈕放在兩個線性布局下方? (由於weight屬性占用相同空間android:layout_weight="1")
這是我正在使用的代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="@string/team_a"
android:layout_gravity="center_horizontal"
tools:layout_editor_absoluteX="175dp"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/team_a_score"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:padding="8dp"
android:textAlignment="center"
android:text=""/>
<Button
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/_3_points"
android:textStyle="bold"
android:onClick="points3"
android:textAllCaps="true"/>
<Button
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:onClick="points2"
android:textAllCaps="true"
android:text="@string/_2_points" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textStyle="bold"
android:textAllCaps="true"
android:onClick="freethrow"
android:text="@string/free_throw" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="@string/team_b"
android:layout_gravity="center_horizontal"
tools:layout_editor_absoluteX="175dp"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/team_a_score1"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:padding="8dp"
android:textAlignment="center"
android:text=""/>
<Button
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/_3_points"
android:textStyle="bold"
android:onClick="points31"
android:textAllCaps="true"/>
<Button
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="@+id/button21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:onClick="points21"
android:textAllCaps="true"
android:text="@string/_2_points" />
<Button
android:id="@+id/button31"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textStyle="bold"
android:textAllCaps="true"
android:onClick="freethrow1"
android:text="@string/free_throw" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
提前致謝。
還沒有測試過這個,但應該可以正常工作
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="@string/team_a"
android:layout_gravity="center_horizontal"
tools:layout_editor_absoluteX="175dp"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/team_a_score"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:padding="8dp"
android:textAlignment="center"
android:text=""/>
<Button
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/_3_points"
android:textStyle="bold"
android:onClick="points3"
android:textAllCaps="true"/>
<Button
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:onClick="points2"
android:textAllCaps="true"
android:text="@string/_2_points" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textStyle="bold"
android:textAllCaps="true"
android:onClick="freethrow"
android:text="@string/free_throw" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="@string/team_b"
android:layout_gravity="center_horizontal"
tools:layout_editor_absoluteX="175dp"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/team_a_score1"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:padding="8dp"
android:textAlignment="center"
android:text=""/>
<Button
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/_3_points"
android:textStyle="bold"
android:onClick="points31"
android:textAllCaps="true"/>
<Button
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="@+id/button21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:onClick="points21"
android:textAllCaps="true"
android:text="@string/_2_points" />
<Button
android:id="@+id/button31"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textStyle="bold"
android:textAllCaps="true"
android:onClick="freethrow1"
android:text="@string/free_throw" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout_gravity="center"
android:text="Button" />
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.