简体   繁体   中英

Android - restrict space to take match_parent and views on both sides

I have a horizontal linearLayout including a TextView, an EditText and another EditText. Both TextViews are set to wrap_content in width and heigth. The EditText has wrap_content in height and match_parent in width.

My goal is to get those three views in one line filling the whole line. But with the above written settings, I have the first TextView taking up the needed space and the EditText filling the rest, not keeping free space for the other TextView.

I achieved a temporary solution by setting layout_weight of both TextViews to 0 and of the EditText to 500 which I'm sure is not the real solution.

Edit: current layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayoutWeight"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gewicht:"
        android:id="@+id/lblWeight"
        android:layout_alignParentStart="true"
        android:textColor="@color/mainForeground"
        android:textSize="24dp"
        android:layout_weight="1"
        android:textStyle="bold" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtWeight"
        android:layout_alignBottom="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:textAlignment="center"
        android:hint="60"
        android:textSize="24dp"
        android:textColor="@color/mainForeground"
        android:textColorHint="@color/hintColor"

        android:layout_weight="500"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lblUnitWeight"
        android:textColor="@color/mainForeground"
        android:textSize="24dp"
        android:layout_weight="1"
        android:text="g." />
</LinearLayout>

Use layout_weight to determine width of each views. Update the weight values according to your need.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayoutWeight"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Gewicht:"
        android:id="@+id/lblWeight"
        android:layout_alignParentStart="true"
        android:textColor="@color/mainForeground"
        android:textSize="24dp"
        android:layout_weight="1"
        android:textStyle="bold" />

    <EditText
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="wrap_content"
        android:id="@+id/txtWeight"
        android:layout_alignBottom="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:textAlignment="center"
        android:hint="60"
        android:textSize="24dp"
        android:textColor="@color/mainForeground"
        android:textColorHint="@color/hintColor"

        android:layout_weight="500"/>
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/lblUnitWeight"
        android:textColor="@color/mainForeground"
        android:textSize="24dp"
        android:layout_weight="1"
        android:text="g." />
</LinearLayout>

I'm a little bit consufed, but is this what you want?

在此处输入图片说明

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

    <EditText
        android:id="@+id/editText"
        android:layout_toEndOf="@+id/leftTextView"
        android:layout_toStartOf="@+id/rightTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content/>

    <TextView
        android:id="@+id/rightTextView"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

Try this, it should works fine.

You can use this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayoutWeight"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gewicht:"
        android:id="@+id/lblWeight"
        android:layout_alignParentStart="true"
        android:textColor="@color/mainForeground"
        android:textSize="24dp"
        android:textStyle="bold" />

    <EditText
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/txtWeight"
        android:layout_alignBottom="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:textAlignment="center"
        android:hint="60"
        android:textSize="24dp"
        android:textColor="@color/mainForeground"
        android:textColorHint="@color/hintColor"
        android:layout_weight="500"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lblUnitWeight"
        android:textColor="@color/mainForeground"
        android:textSize="24dp"
        android:text="g." />

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM