ImageView的android:maxHeight,android:minHeight的正确设置

本文介绍如何在Android布局文件中正确设置ImageView的最大和最小高度及宽度。要实现这些设置,需要使用android:adjustViewBounds属性并将其值设为true。此外,在代码中也需要调用setAdjustViewBounds方法并传入true参数。

在layout文件中,设置IamgeView的最大(最小)高度(宽度)时,需要同时设置android:adjustViewBounds="true",这样设置才会生效。在代码中设置时,需要setAdjustViewBounds为true。一个layout的实例:

 

[html]  view plain copy
  1. android:adjustViewBounds="true"  
  2. android:maxHeight="150.0dip"  
  3. android:maxWidth="150.0dip"  
  4. android:minHeight="33.0dip"  
  5. android:minWidth="48.0dip"  
<?xml version="1.0" encoding="utf-8"?> <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" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".PresetFragment"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="420dp" android:layout_marginEnd="16dp" android:layout_marginStart="16dp" android:layout_marginTop="6dp"> <ImageView android:id="@+id/back" android:layout_width="24dp" android:layout_height="24dp" android:src="@drawable/auto_close" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toEndOf="@+id/back" android:text="Edit Preset" android:textSize="20sp" android:textColor="@color/black" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="@+id/back" android:layout_marginStart="5dp"/> <ImageView android:id="@+id/done" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent" android:src="@drawable/auto_ok"/> <androidx.cardview.widget.CardView android:id="@+id/button_group" android:layout_width="250dp" android:layout_height="35dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/done" android:layout_marginTop="20dp" app:cardCornerRadius="17dp" android:backgroundTint="#F7F7F7"> <!-- 使用 LinearLayout 来水平排列两个按钮 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:baselineAligned="false"> <!-- 防止 baseline 对齐干扰 --> <com.google.android.material.button.MaterialButton android:id="@+id/white_light" android:layout_width="0dp" android:layout_weight="1" android:layout_height="35dp" android:backgroundTint="@color/blue" android:text="White Light" android:textColor="@color/white" android:gravity="center" android:paddingTop="0dp" android:paddingBottom="0dp" app:cornerRadius="17dp" android:textSize="12sp" /> <com.google.android.material.button.MaterialButton android:id="@+id/color_light" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:backgroundTint="@android:color/transparent" android:textColor="@color/gray_99999" android:text="Color Light" android:textSize="12sp" /> </LinearLayout> </androidx.cardview.widget.CardView> <TextView android:id="@+id/temperature_text" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button_group" android:text="Color Temperature" android:textColor="@color/gray_99999" android:layout_marginTop="20dp"/> <com.example.tapobulb.view.ColorPickerView android:id="@+id/colorPickerView" android:layout_width="250dp" android:layout_height="250dp" android:src="@drawable/color_select" app:layout_constraintTop_toBottomOf="@+id/temperature_text" android:layout_marginTop="40dp" android:visibility="visible" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <ImageView android:id="@+id/color_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/color_select" app:layout_constraintTop_toBottomOf="@+id/temperature_text" android:layout_marginTop="38dp" android:visibility="gone" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <TextView android:id="@+id/color_temperature" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/button_group" android:text="2700K" android:textColor="@color/gray333" android:layout_marginTop="20dp"/> <TextView android:id="@+id/color_value" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/button_group" android:visibility="visible" android:text="xxxx" android:textColor="@color/gray333" android:layout_marginTop="20dp"/> <TextView android:id="@+id/warm" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/color_temperature" android:text="Warm" android:textColor="@color/gray_99999" android:textSize="14sp" android:layout_marginTop="24dp"/> <com.example.tapobulb.view.DashedLineView android:id="@+id/line1" android:layout_width="wrap_content" android:layout_height="9dp" android:background="@drawable/dashed_line" app:layout_constraintStart_toEndOf="@id/warm" app:layout_constraintTop_toBottomOf="@+id/color_temperature" android:layout_marginStart="20dp" android:layout_marginTop="26dp"/> <TextView android:id="@+id/recommend" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/warm" android:text="Recommend" android:textColor="@color/gray_99999" android:layout_marginTop="46dp"/> <com.example.tapobulb.view.DashedLineView android:id="@+id/line2" android:layout_width="wrap_content" android:layout_height="9dp" android:background="@drawable/dashed_line" app:layout_constraintStart_toEndOf="@id/recommend" app:layout_constraintTop_toBottomOf="@+id/warm" android:layout_marginStart="20dp" android:layout_marginTop="48dp"/> <TextView android:id="@+id/white" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/recommend" android:text="White" android:textColor="@color/gray_99999" android:layout_marginTop="46dp"/> <com.example.tapobulb.view.DashedLineView android:id="@+id/line3" android:layout_width="wrap_content" android:layout_height="9dp" android:background="@drawable/dashed_line" app:layout_constraintStart_toEndOf="@id/white" app:layout_constraintTop_toBottomOf="@+id/recommend" android:layout_marginStart="20dp" android:layout_marginTop="48dp"/> <TextView android:id="@+id/daylight" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/white" android:text="Daylight" android:textColor="@color/gray_99999" android:layout_marginTop="46dp"/> <com.example.tapobulb.view.DashedLineView android:id="@+id/line4" android:layout_width="wrap_content" android:layout_height="9dp" android:background="@drawable/dashed_line" app:layout_constraintStart_toEndOf="@id/daylight" app:layout_constraintTop_toBottomOf="@+id/white" android:layout_marginStart="20dp" android:layout_marginTop="48dp"/> <TextView android:id="@+id/cool" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/daylight" android:text="Cool" android:textColor="@color/gray_99999" android:layout_marginTop="46dp"/> <com.example.tapobulb.view.DashedLineView android:id="@+id/line5" android:layout_width="wrap_content" android:layout_height="9dp" android:background="@drawable/dashed_line" app:layout_constraintStart_toEndOf="@id/cool" app:layout_constraintTop_toBottomOf="@+id/daylight" android:layout_marginStart="20dp" android:layout_marginTop="48dp"/> <ImageView android:id="@+id/color_bar" android:layout_width="60dp" android:layout_height="238dp" android:src="@drawable/color_bar" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/color_temperature" android:layout_marginTop="46dp"/> <FrameLayout android:id="@+id/temperature_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/temperature_rectangle" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/color_temperature" android:layout_marginTop="50dp"/> <View android:layout_width="match_parent" app:layout_constraintTop_toBottomOf="@+id/color_temperature" android:layout_marginTop="100dp" android:layout_height="10dp"/> <androidx.constraintlayout.widget.Group android:id="@+id/white_group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" app:constraint_referenced_ids="color_bar,color_temperature,temperature_select,temperature_select,warm,line1,recommend,line2,white,line3,daylight,line4,cool,line5"/> </androidx.constraintlayout.widget.ConstraintLayout> <View android:layout_width="match_parent" android:layout_height="10dp" android:background="@color/bg_gray" android:layout_marginTop="20dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/daylight" android:text="Light" android:textSize="18sp" android:layout_marginStart="16dp" android:textColor="@color/gray_99999" android:layout_weight="1" android:layout_marginTop="46dp"/> <TextView android:id="@+id/light_percent" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/daylight" android:text="64%" android:textSize="18sp" android:layout_marginStart="16dp" android:textColor="@color/gray333" android:layout_marginEnd="16dp" android:layout_marginTop="46dp"/> </LinearLayout> <SeekBar android:id="@+id/seekBar" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:progressDrawable="@drawable/custom_seekbar_progress" android:thumb="@drawable/seekbar_icon_resized" android:splitTrack="false" android:minHeight="4dp" android:maxHeight="4dp" android:max="100" android:progress="50" /> <View android:layout_width="match_parent" android:layout_height="30dp"/> </LinearLayout>这是我的布局,应该修改哪里li
08-26
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- 标题栏 --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="#44bd32"> <Button android:id="@+id/fanHui" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@android:color/transparent" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="16sp" /> </RelativeLayout> <!-- 底部按钮 --> <LinearLayout android:id="@+id/footer_buttons" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:orientation="horizontal"> <!-- <Button--> <!-- android:id="@+id/device_location"--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="match_parent"--> <!-- android:gravity="center"--> <!-- android:text="设备定位"--> <!-- android:textSize="19sp"--> <!-- android:background="#44bd32"--> <!-- android:textColor="@android:color/white" />--> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="380dp" android:layout_above="@id/footer_buttons" android:layout_below="@id/toolbar" android:layout_marginBottom="298dp" android:background="@android:color/black" android:padding="0dp"> <!-- 视频播放器 --> <VideoView android:id="@+id/video_player" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" /> <!-- 播放控制条 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#99000000" android:orientation="horizontal" android:padding="8dp"> <ImageButton android:id="@+id/btn_play_pause" android:layout_width="40dp" android:layout_height="40dp" android:background="?attr/selectableItemBackgroundBorderless" android:src="@drawable/ic_play_sel" /> <SeekBar android:id="@+id/video_seekbar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="00:00/00:00" android:textColor="@android:color/white" android:textSize="14sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout> 在视频播放器模块下面屏幕剩余位置添加一个放置缩略图的模块<ImageView android:layout_width="0dp" android:layout_height="wrap_content" app:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="萤石缩略图" android:src="@drawable/default_thumbnail" />
06-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值