minHeight не работает с Relative Layout - PullRequest
0 голосов
/ 31 октября 2018

Я использовал ImageView и Textview внутри RelativeLayout.

Что я ожидаю:

  1. Высота TextView должна быть wrap_content
  2. Просмотр изображения должен заполнить оставшееся пространство
  3. Минимальная высота Imageview составляет 250dp.

XML-файл

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:minHeight="250dp"
        android:layout_above="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/facebookshare">

    </ImageView>
    <TextView
        android:layout_alignParentBottom="true"
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

</RelativeLayout>

Вывод: (высота просмотра изображения становится равной 0)

enter image description here

Ожидается: (Высота просмотра изображения должна быть> 250 dp // minHeight = 250dp)

enter image description here

Когда Контент маленький, Imageview должен заполнить экран как

enter image description here

Может ли кто-нибудь мне помочь !!!

Ответы [ 6 ]

0 голосов
/ 31 октября 2018

Попробуйте вот так

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/app_name"
        android:minHeight="250dp"
        android:layout_above="@+id/tvText"
        android:src="@drawable/kid" />


    <TextView
        android:id="@+id/tvText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorAccent"
        android:text="@string/large_text" />


</RelativeLayout>

OUTPUT

Когда большой текст

enter image description here

Когда маленький текст

enter image description here

UPDATE

<string name="large_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce pulvinar nec justo id bibendum.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce pulvinar nec justo id bibendum.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce pulvinar nec justo id bibendum.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce pulvinar nec justo id bibendum.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce pulvinar nec justo id bibendum.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis bibendum mattis risus eget pulvinar. Praesent commodo erat enim, id congue sem tristique vitae. Proin vitae accumsan justo, ut imperdiet tellus. Mauris neque nibh, hendrerit id tortor vel, congue sagittis odio. Morbi elementum lobortis maximus. Etiam sit amet porttitor massa. Fusce sed magna quis arcu tincidunt finibus vitae id erat. Pellentesque massa mi, imperdiet eget accums</string>    <!-- TODO: Remove or change this placeholder text -->

    <string name="small_text">Hello World Lorem ipsum dolor sit amet, consectetur adipiscing elit
    Hello World Lorem ipsum dolor sit amet, consectetur adipiscing elit
    Hello World Lorem ipsum dolor sit amet, consectetur adipiscing elit</string>
0 голосов
/ 31 октября 2018

Попробуйте, это работает отлично. Используйте layout_weight. установите maxWeight для изображения и оставшийся вес для текста. При увеличении длины текста уменьшается высота изображения. Это зависит от длины предоставленного вами текста.

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        tools:srcCompat="@tools:sample/backgrounds/scenic"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:text="Hhhjkahsgkjhasd jasdhfkjsd hfdjkshfuernf jsdhfsudf jdhfkjdshf jhdfjshf kjdhf kjdhf kajhdf  jhsgdfs jd sdgfjkgf sjdgfs jfshdg "/>
</LinearLayout>
0 голосов
/ 31 октября 2018

Заключите ImageView в другой макет:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/linear">

        <ImageView
            android:minHeight="250dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/what"/>
    </LinearLayout>

    <TextView
        android:layout_alignParentBottom="true"
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>
0 голосов
/ 31 октября 2018

Используйте следующий код, если вы используете ConstraintLayout, если нет, пожалуйста, сообщите мне об этом. Нужно использовать NestedScrollView:

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintHeight_min="250dp"
            android:src="@drawable/ic_launcher_background"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/txt" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="text"
            app:layout_constraintTop_toBottomOf="@id/img"
            app:layout_constraintBottom_toBottomOf="parent" />

    </android.support.constraint.ConstraintLayout>
0 голосов
/ 31 октября 2018

minHeight из ImageView не работает только потому, что вы установили высоту ImageView как match_parent. Таким образом, он получает достаточно места (более 250 dp для рендеринга и игнорирует minHeight).

Просто измените android:layout_height="match_parent" на android:layout_height="wrap_content"

<ImageView
      android:layout_above="@+id/linear"
      android:minHeight="250dp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:src="@drawable/facebookshare">

Рабочий образец макета

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="sdvdsibvdsv"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/facebookshare"
        app:layout_constraintBottom_toTopOf="@+id/linear"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_min="250dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Возможно, представление макета покажет вам неверный рендеринг пользовательского интерфейса. Просто запустите это. Это будет работать.

0 голосов
/ 31 октября 2018

Это просто взлом.

Вместо minHeight на ImageView, вы можете указать maxLines на TextView. Вы также можете иметь размер TextView на концах, которые тоже будут хорошо смотреться.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleType="centerCrop"
        android:src="@drawable/img_cat">
    </ImageView>

    <TextView
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="5"
        android:scrollbars="vertical"
        android:textSize="40sp"
        tools:text="asdlfkasdfkasdf"
        />

</LinearLayout>

img

...