简体   繁体   中英

Linear Layout going out of bounds

I have the following layout as a RecyclerView item, composed of an image and a text:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/library_list_element"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:src="@drawable/ic_delete_black_18dp"
        >
    </ImageButton>

    <TextView
        android:id="@+id/folderName"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:maxLines="3"
        android:minLines="1"
        android:textAppearance="?attr/textAppearanceBody1"
        android:textSize="14sp"
        tools:text="/sdcard/somepath/somewhere" />


</LinearLayout>

I want to put the button after the text. If I invert the order of the Text/Button, the text is taking all the space and the button goes out of bounds. I know I can specify the size of text manually, but that's not very clean. How do you do to have the following layout? :

<--------------------------Text--------------------------><-----Button----->
                         Wrapping content                       50dp

You can use layout_weight="1" on TextView to achieve this.

<TextView
    android:id="@+id/folderName"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:maxLines="3"
    android:minLines="1"
    android:textAppearance="?attr/textAppearanceBody1"
    android:textSize="14sp"
    tools:text="/sdcard/somepath/somewhere" />

<ImageButton
    android:id="@+id/button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:src="@drawable/ic_delete_black_18dp" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/library_list_element"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">


<TextView
    android:id="@+id/folderName"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"            
    android:gravity="center"
    android:maxLines="3"
    android:minLines="1"
    android:textAppearance="?attr/textAppearanceBody1"
    android:textSize="14sp"
    tools:text="/sdcard/somepath/somewhere" />

<ImageButton
    android:id="@+id/button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:src="@drawable/ic_delete_black_18dp"/>
</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