代码之家  ›  专栏  ›  技术社区  ›  Jyoti JK

minHeight不使用相对布局

  •  1
  • Jyoti JK  · 技术社区  · 7 年前

    我在RelativeLayout中使用了ImageView和Textview。

    我期待的是:

    1. 文本视图的高度应为包裹内容
    2. Imageview应该填满剩余的空间
    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>
    

    输出:(Imageview高度变为0)

    enter image description here

    预期:(图像视图高度应大于250 dp//minHeight=250 dp)

    enter image description here

    当内容很小时,Imageview应该像

    enter image description here

    谁能帮帮我!!!

    6 回复  |  直到 7 年前
        1
  •  1
  •   Khemraj Sharma    7 年前

    如果正在使用,请使用以下代码 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>
    
        2
  •  1
  •   AskNilesh    7 年前

    这样试试

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

    输出

    当大文本

    enter image description here

    当小文本

    enter image description here

    使现代化

    <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>
    
        3
  •  -1
  •   Dejene T.    7 年前

    试试这个,效果很好。使用布局和重量。设置图像的最大权重和文本的剩余权重。当文本长度增加时,会降低图像高度。这取决于你提供的文本的长度。

    <?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>
    
        4
  •  0
  •   forpas    7 年前

    将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>
    
        5
  •  0
  •   Khemraj Sharma    7 年前

    这只是一次黑客攻击。

    而不是给予 minHeight 到 ImageView ,你可以给 maxLines 到 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

        6
  •  0
  •   Pankaj Kumar    7 年前

    minHeight 属于 ImageView 不工作只是因为你设定了 图片框 像 match_parent 因此,它获得了足够的空间(超过250dp)来渲染和忽略 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>
    

    布局设计视图可能会显示不正确的UI呈现。运行它。它会起作用的。