Adam Staszak
Adam Staszak

Reputation: 1704

How to put object in LinearLayout in a space left by other object?

i'm trying to build a layout. I use horizontal LinearLayout, inside of it I have one imageview, another layout and another imageview. The point is that two imageviews should be on the left and right side and have size od 10 dp. Center LinearLayout should be stretched to the screen but without 10dp on the left and right.

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

        <ImageView
            android:id="@+id/li_iv"
            android:layout_width="10dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="@drawable/pen" />

        <LinearLayout
            android:id="@+id/li_ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:background="@drawable/back"
            android:orientation="vertical" >

            <TextView
                android:id="@android:id/title"
                android:layout_width="match_parent"
                android:layout_height="28dp"
                android:layout_marginLeft="28dp"
                android:layout_marginRight="28dp"
                android:textSize="22sp"
                android:textStyle="bold" />

            <TextView
                android:id="@android:id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="29dp"
                android:layout_marginRight="31dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/back_repeat"
                android:text="aaaaaaaaaa\n"
                android:textSize="16dp" />
        <ImageView
            android:id="@+id/li_rl_center_right"
            android:layout_width="10dp"
            android:layout_alignParentRight="true"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="@drawable/pen" />
        </LinearLayout>

But that doesn't happen. Right side imageview is outside of boundary of main LinearLayout - on the right side of it, so actually outside of the screen because main LinearLayout width fills parent. I do not use relativeLayout because then content isn't stretched to the size of parents, but to the size of whole screen in Graphical Layout in eclipse, and on the phone imageViews have only default size of parent. When inside of textview there are more lines, then images on the side do not stretch.

Any ideas how I can put back this ImageView on the right side back in Layout?

Upvotes: 1

Views: 1983

Answers (3)

Emirhan Eyidoğan
Emirhan Eyidoğan

Reputation: 11

I use a hidden button for space.

<LinearLayout
    android:id="@+id/buttons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="2dp"
    android:layout_marginEnd="5dp"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Text"
        android:textSize="14sp"
        android:visibility="visible" />

    <!-- Non-Visible Button For Space: "Weight" and "Visibility" are important. -->
    <Button
        android:id="@+id/space"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Button"
        android:visibility="invisible" />

    <ImageButton
        android:id="@+id/other"
        style="@style/MiniPlayerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:padding="10dp"
        app:srcCompat="@drawable/ic_other"
        android:textAllCaps="false"
        android:textSize="14sp"
        android:visibility="visible"
        android:contentDescription="Desc" />
</LinearLayout>

Upvotes: 0

Chiral Code
Chiral Code

Reputation: 1436

You should use relative layout, please try this code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/li_iv"
        android:layout_width="10dp"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/li_ll"
        android:layout_alignParentLeft="true"
        android:background="@drawable/pen" />

    <LinearLayout
        android:id="@+id/li_ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/li_rl_center_right"
        android:layout_toRightOf="@+id/li_iv"
        android:background="@drawable/back"
        android:orientation="vertical" >

        <TextView
            android:id="@android:id/title"
            android:layout_width="match_parent"
            android:layout_height="28dp"
            android:layout_marginLeft="28dp"
            android:layout_marginRight="28dp"
            android:textSize="22sp"
            android:textStyle="bold" />

        <TextView
            android:id="@android:id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="29dp"
            android:layout_marginRight="31dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/back_repeat"
            android:text="aaaaaaaaaa\n"
            android:textSize="16dp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/li_rl_center_right"
        android:layout_width="10dp"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/li_ll"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:background="@drawable/pen" />

</RelativeLayout>

Upvotes: 2

Cote Mounyo
Cote Mounyo

Reputation: 13965

You need to set everything to android:layout_width="match_parent" and then use weight to specify how much of the screen each takes.

Upvotes: 0

Related Questions