Question

For the below layout of controls, I am using relative layout with 2 linear layouts. 1 Linear Layout for B1 ,B2 and B3 with grey background and 1 Linear Layout for B4, B5 and B6.

I have read that using Linear Layouts inside Relative Layouts slows the rendering.

Any ideas how I can get rid of Linear Layouts? Especially I cannot get the grey background for the Row B1, B2 and B3. Or do you think it should be fine using this way. Thanks in advance for any help.

enter image description here

EDIT :

Below is my xml which I just started building and trying to get rid of any child layouts if possible.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/abc_ic_search_api_holo_light" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_below="@+id/imageButton1"
        android:layout_centerHorizontal="true"
        android:text=" Test Label" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="54dp"
        android:background="@android:color/darker_gray" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_toRightOf="@+id/linearLayout1"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_marginLeft="18dp"
            android:layout_toRightOf="@+id/imageView1"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button2"
            android:layout_alignBottom="@+id/button2"
            android:layout_toRightOf="@+id/button2"
            android:text="Button" />
    </LinearLayout>



    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginTop="31dp" >

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/button5"
            android:layout_toLeftOf="@+id/imageButton1"
            android:text="Button" />

        <Button
            android:id="@+id/button4"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/linearLayout2"
            android:layout_centerVertical="true"
            android:text="Button" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/button4"
            android:text="Button" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_alignTop="@+id/listView1"
        android:layout_marginRight="15dp"
        android:text="TextView" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout2"
        android:layout_marginTop="18dp" >
    </ListView>    

</RelativeLayout>
Was it helpful?

Solution

Without seeing your xml, the only way you will be able to accomplish this, is with is with a single relative layout, that contains another LinearLayout containing your row of buttons.

While you are correct, that it is good practice to limit the number of nested layouts (not just linear, but nested layouts of any type).

In this case, to get your row background set, you will need at least a single nested LinearLayout, and this is not a big issue.

Edit: After reviewing your XML, I do think this is the best way to achieve what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top