Aligning controls next to each other in relative layout

I have 2 image buttons and 2 text boxes. I want to place them in this order.

textview1 - imagebutton1 - textview2 - imagebutton2

Can anyone here help me with this?

this is what i tried but it doesn't work properly

<TextView
       android:id="@+id/like_count"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        android:layout_below="@id/comments_list"
        android:textColor="@android:color/white"
        android:textSize="14sp" 
        android:text="10" />

      <ImageButton
        android:id="@+id/like_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_below="@id/comments_list"
        android:layout_toRightOf="@id/like_count"
        android:background="@android:color/transparent"
        android:src="@drawable/xml_like_button_selctor" />

      <TextView
       android:id="@+id/comment_count"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/like_button"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        android:text="10"/> 

     <ImageButton
        android:id="@+id/comment_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/comment_count"
        android:background="@android:color/transparent"
        android:src="@drawable/xml_comment_button_selector" />
+4
source share
4 answers

Try it.

You can use LinearLayout android:orientation="horizontal"all child views android:layout_width="0dp"andandroid:layout_weight="1"

<LinearLayout
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/like_count"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:text="10"
        android:textColor="@android:color/white"
        android:textSize="14sp" />

    <ImageButton
        android:id="@+id/like_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:src="@drawable/xml_like_button_selctor" />

    <TextView
        android:id="@+id/comment_count"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="5dp"
        android:text="10"
        android:textColor="@android:color/white"
        android:textSize="14sp" />

    <ImageButton
        android:id="@+id/comment_icon"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:src="@drawable/xml_comment_button_selector" />

</LinearLayout>
+7
source

Please use the following relative layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#EAEAEA" >

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text1"
        android:id="@+id/text1"
        />

    <ImageButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgButton1"
        android:layout_toRightOf="@+id/text1"/>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text2"
        android:id="@+id/text2"
        android:layout_toRightOf="@+id/imgButton1"
        />

    <ImageButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/text2"
        android:id="@+id/imgButton2"/>


</RelativeLayout>
+4
source

Why aren't you using LinerLayout for this.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sliding_pane_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text1" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text1" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>
0
source

Change the width of the TextView to wrap_content

-1
source

Source: https://habr.com/ru/post/1541403/


All Articles