简体   繁体   中英

android studio relativelayout

new to Android studio and Java. I am looking to use a relativelayout to produce this:- Layout required

For some reason can not get to work. If layout is change, ie rotated 90 degrees right. This giving the image along the top with the four buttons underneath it work. But can not get the image on the left with button on the right.

Please any help.

You can use LinearLayout instead of 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>

Easy to produce that layout using LinearLayout than Relative, something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM