简体   繁体   中英

Make LinearLayout adjust it's height to a TextView with “wrap_content” height

I have a LinearLayout containing an ImageView with fixed height, and a TextView with wrap_content height. I want the LinearLayout to grow to fit the entire TextView if the text doesn't fit on one line.

My code:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="vertical">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="5dp"
        android:scaleType="fitCenter"
        android:src="@drawable/question_mark"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:singleLine="false"
        android:text="Some long text that doesnt fit on one line"/>
</LinearLayout>

The LinearLayout in the example above doesn't resize, so I only see "Some long text" and then the rest of the TextView is hidden.

Any way to get the LinearLayout to resize according to the TextView ?

I know I can use RelativeLayout and other types of layouts to achieve this, but I'm asking about a solution using LinearLayout .

Try this :

在此处输入图片说明

<LinearLayout android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="5dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher_background"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Some long text that doesnt fit on one line"/>

</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