Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
55 views
in Technique[技术] by (71.8m points)

java - Align Grid in RelativeLayout

I am a novice in android studio and after trying different layouts with not really satisifying success, I am now back to Relative Layout. I want to have grid of buttons and the alignments work okay when the buttons are aligned to the end of start of a button the same size, but sometimes I want two smaller buttons underneath a bigger button, but since I can only align the left smaller button with the start of the bigger button and the right smaller button with the end of the end of the bigger button, the space between the smaller buttons is not aligned and causes all kind of problems. How do I align those two buttons in the middle?

Here is my manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <Button
        android:id="@+id/button"
        android:layout_width="26dp"
        android:layout_height="34dp"
        android:layout_alignTop="@id/enterYouText"
        android:layout_alignParentEnd="true"
        android:layout_toRightOf="@+id/enterYouText"
        android:background="@null"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:text="SPEAK" />


<ImageButton
        android:id="@+id/throat"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_below="@id/secondPage"
        android:layout_alignTop="@id/head"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="3dp"
        android:layout_marginEnd="0dp"
        android:layout_toRightOf="@id/kopf"
        android:adjustViewBounds="false"
        android:backgroundTint="#FAF6F6"
        android:scaleType="centerInside"
        android:src="@mipmap/picture" />

    <ImageButton
        android:id="@+id/head"
        android:layout_width="110dp"
        android:layout_height="110dp"
        android:layout_below="@id/secondPage"
        android:layout_alignStart="@id/button"
        android:layout_alignTop="@id/xy"
        android:layout_alignParentEnd="false"
        android:adjustViewBounds="false"
        android:backgroundTint="#FAF6F6"
        android:scaleType="centerInside"
        android:src="@mipmap/picture" />



    <Button
        android:id="@+id/secondPage"

        android:layout_width="225dp"
        android:layout_height="23dp"

        android:layout_below="@id/button"
        android:layout_alignStart="@id/button"
        android:layout_alignBottom="@id/enterYouText"
        android:layout_alignParentStart="false"
        android:layout_alignParentTop="false"
        android:layout_alignParentEnd="true"
        android:background="@drawable/buttonsmall"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text="Second Page"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />

</RelativeLayout>
question from:https://stackoverflow.com/questions/65897996/align-grid-in-relativelayout

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, what you have to do to align center(horizontally and vertically which means middle of whole screen).

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

alternatively,

android:layout_centerInParent="true"

This way you can put buttons on center. Suppose, you put a button(called A) on center. Now, you wanna put another button beside that button. Than you have to write

android:layout_toRightOf="@+id/A"

above code in button B. If you want to put another button on left of button A. Then, try

android:layout_toLeftOf="@+id/A"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...