安卓工作室相对布局



Android studio和Java的新手。我想用一个相对的布局来制作这个:-所需布局

由于某些原因无法上班。如果布局发生变化(向右旋转90度(。这使图像沿着顶部和下面的四个按钮工作。但无法通过右侧的按钮获取左侧的图像。

请帮忙。

您可以使用LinearLayout而不是relativelayout。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 2" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 3" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 4" />
</LinearLayout>
</LinearLayout>

使用LinearLayout比使用Relative更容易生成该布局,类似于以下内容:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:weightSum="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="20dp"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:text="Image"
android:layout_weight="1">
</Button>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="4"
android:padding="20dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"></Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"></Button>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

最新更新