Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: LinearView with fixed sized widgets left and right and a flexible one in the middle

Seems to be a simple question, but I can't figure out how to do it.

I have a horizontal LinearView with something of fixed size at the left, then comes some text which should take as mich space as possible, but on the right should be another fixed-size object.

Now, my problems are right now: When the text in the middle is too long, the object at the right gets pushed out of the window. When the text is too short, it does not sit at the right border of the window, but directly next to the text.

How can I do it? My Code so far (As you can see, I tried to use the LayoutWeight, which didn't help...):

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

    <TextView
        android:id="@+id/important"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp"
        android:background="#000000"
        android:layout_weight="1"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:paddingRight="5dp"
            android:paddingLeft="0dp"
            android:textColor="#333333"
            android:paddingBottom="0dp"
            android:textSize="14sp"
            android:scrollHorizontally="true"/>
        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#666666"
            android:paddingTop="3dp"
            android:paddingRight="5dp"
            android:paddingLeft="10dp"
            android:paddingBottom="5dp"
            android:textSize="12sp" />
    </LinearLayout>

    <ImageView android:id="@+id/lastpage" 
        android:src="@drawable/lastpage"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="200"
        android:scaleType="center"
        android:layout_gravity="right"/>
</LinearLayout>
like image 777
janoliver Avatar asked Dec 09 '22 12:12

janoliver


2 Answers

Looking at this answer Android: LinearView with fixed sized widgets left and right and a flexible one in the middle you maybe just need to set the layout_width of your middle element to 0px to make it not use the width gained by content but decide about it via the weight.

Hope this helps.

like image 114
sunadorer Avatar answered Jan 14 '23 07:01

sunadorer


You have android:layout_weight="1" on two widgets (first and second children of the LinearLayout), which does not match your business rules.

Personally, I would use a RelativeLayout here. Use android:layout_alignParentLeft="true" on the first child, android:layout_alignParentRight="true" on the third child, and android:toLeftOf and android:toRightOf on the middle, anchoring it between the outer fixed children. That more explicitly implements your desired business rules.

like image 33
CommonsWare Avatar answered Jan 14 '23 07:01

CommonsWare