android:scaleType设置失效的问题解决

本文将详细解释为何在Android布局XML文件中,当图片资源未放置在drawable/hdpi目录时,使用ImageView的scaleType属性无法正常显示缩放效果,并提供了解决方案。通过调整图片资源存放位置、正确使用src和background属性,以及确保资源符合hdpi要求,可以解决这一问题。
部署运行你感兴趣的模型镜像


图片必须放在 drawable/hdpi 目录中

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="30dp"      
        android:src="@drawable/sy_05"
        android:scaleType="centerInside" />

在layout xml布局中,看似一个简简单单的imageView定义,结果发现android scalType一直没有效果。

解决方法:

1、设置背景图要用android:src而不是android:background

2、若在代码中设置要用imageView.setImageResource()而不是setBackgroundResource()

3、也是最难发现的一个,就是图片资源必须放在 drawable/hdpi 目录中


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

<?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="ExtraText"> <!--图片按钮--> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_server"/> <!--开关按钮--> <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!--选择按钮--> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!--下拉列表--> <Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="@color/black"/> <!--引用自定义形状--> <!-- <ImageView android:src="@drawable/sb" 这个属性的作用是让视图能够响应点击事件。 android:clickable="true" --> 这个属性的功能是将视图的点击事件和 Activity 里的方法关联起来。 android:onClick="loginWithQQ" --> 使用 CardView 实现圆角和阴影效果 每个按钮使用CardView作为容器 通过app:cardCornerRadius="12dp"设置圆角 通过app:cardElevation="4dp"添加阴影 设置app:cardBackgroundColor替代原有的backgroundTint <!--<权重分配> android:layout_weight="1" android:layout_weight="2" android:layout_weight="3" </权重分配>--> <!--垂直下滑格式--> <!--垂直下滑容器--> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000000000" android:textSize="20sp" android:textColor="@color/black"/> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@drawable/hutao" android:scaleType="centerCrop"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000000000" android:textSize="20sp" android:textColor="@color/black"/> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@drawable/hutao" android:scaleType="centerCrop"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000000000" android:textSize="20sp" android:textColor="@color/black" /> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@drawable/hutao" android:scaleType="centerCrop"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000000000" android:textSize="20sp" android:textColor="@color/black"/> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@drawable/hutao" android:scaleType="centerCrop"/> </LinearLayout> </ScrollView> <!--垂直下滑容器--> </LinearLayout>错误分析
08-02
<?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" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:title="" app:navigationIcon="@drawable/back"> </androidx.appcompat.widget.Toolbar> <ImageView android:layout_width="match_parent" android:layout_height="180dp" android:src="@mipmap/picture_8" android:scaleType="centerCrop"/> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_margin="10dp" android:orientation="vertical" android:layout_height="wrap_content"> </androidx.appcompat.widget.LinearLayoutCompat> <TextView android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="泰山,又名岱山、岱宗、岱岳、东岳、泰岳,为五岳之一,有“五岳之首”、“五岳独尊”、“天下第一山”之称,被中外学者称为“中国的奥林匹斯山”,位于山东省中部,隶属于泰安市,绵亘于泰安、济南、淄博三市之间,总面积25000公顷,主峰玉皇顶海拔约1545米"/> </androidx.appcompat.widget.LinearLayoutCompat> </RelativeLayout>
06-24
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值