网站首页 > 编程文章 正文
ImageView(图像视图),就是用来显示图像的一个View或者说控件。
Java代码中设置blackground和src属性:
前景(对应src属性):setImageDrawable( );
背景(对应background属性):setBackgroundDrawable( );
ImageView iv_scale = findViewById(R.id.image_01);
// 延時操作
new Handler().postDelayed(() -> {
// TODO
iv_scale.setImageResource(R.drawable.ic_launcher_foreground);
iv_scale.setBackgroundColor(0xff0000ff);
iv_scale.setScaleType(ImageView.ScaleType.CENTER);
}, 3000);
ImageButton 是显示图片的图像按钮,但它继承自ImageView,而非继承Button。
ImageButton 和 Button之间的区别:
ImageButton 可以分别在前景和背景显示图片,只能显示图片不能显示文本,且按比例缩放。
Button 按钮上可显示文本和图片,但通过背景设置的图片会拉伸变形,背景只能显示一张图片
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".ImageViewActivity">
<ImageView
android:id="@+id/image_01"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="5dp"
android:layout_marginBottom="24dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"
tools:ignore="MissingConstraints"
></ImageView>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="match_parent"
android:layout_height="80dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"
app:layout_constraintTop_toBottomOf="@id/image_01"
tools:ignore="MissingConstraints"
></ImageButton>
</androidx.constraintlayout.widget.ConstraintLayout>
如果需要同时展示文本与图像
两种方式
1 利用Layout对ImageView和TextView组合布局
2 通过按钮控件Button的drawable***属性设置文本周围的图标
- drawableTop:指定文字上方的图片
- drawableBottom:指定文字下方的图片
- drawableLeft:指定文字左方的图片
- drawableRight:指定文字右方的图片
- drawablePadding:指定文字和图片的间距
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
ImageView iv_scale = findViewById(R.id.image_01);
// 延時操作
new Handler().postDelayed(() -> {
// TODO
iv_scale.setImageResource(R.drawable.ic_launcher_foreground);
iv_scale.setBackgroundColor(0xff0000ff);
iv_scale.setScaleType(ImageView.ScaleType.CENTER);
}, 3000);
//控制button上图标大小
Button button_01 = findViewById(R.id.button_01);
Drawable drawable1 = getResources().getDrawable(R.drawable.xin_yingxiong);
drawable1.setBounds(0, 0, 40, 40);//第一0是距左边距离,第二0是距上边距离,40分别是长宽
button_01.setCompoundDrawables(drawable1, null, null, null);//只放左边
}
布局
<Button
android:id="@+id/button_01"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:drawableLeft="@drawable/xin_yingxiong"
android:text="图标在左边"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageButton"
android:background="#ffffff"
tools:ignore="MissingConstraints"></Button>
猜你喜欢
- 2024-10-22 产品经理小技术:图片素材随手找,原型设计快又好
- 2024-10-22 Android设置Button按钮的点击,触碰状态效果
- 2024-10-22 AndroidStudio安卓原生开发_UI高级_StateListDrawable状态选择器
- 2024-10-22 Activity初窥门径(初窥门径是什么意思)
- 2024-10-22 Litho的使用及原理剖析(lithiumtools)
- 2024-10-22 Android图片加载框架哪家强?当然是FaceBook出品的Fresco
- 2024-10-22 Android Pixel 预览版(pixel preview)
- 2024-10-22 Android 属性动画资源(android属性动画平移)
- 2024-10-22 Android屏幕适配方案(android 屏幕适配方案)
- 2024-10-22 关于Android系统各个版本之间的新功能
你 发表评论:
欢迎- 06-24一个老爸画了超级有爱的365幅画 | 父亲节献礼
- 06-24产品小白看魏则西事件——用产品思维审视百度推广
- 06-24某教程学习笔记(一):13、脚本木马原理
- 06-24十大常见web漏洞——命令执行漏洞
- 06-24初涉内网,提权那些事(内网渗透提权)
- 06-24黑客命令第16集:47种最常见的**网站方法2/2
- 06-24铭说 | 一句话木马的多种变形方式
- 06-24Java隐藏的10倍效率技巧!90%程序员不知道的魔法方法(附代码)
- 最近发表
- 标签列表
-
- spire.doc (70)
- instanceclient (62)
- solidworks (78)
- system.data.oracleclient (61)
- 按键小精灵源码提取 (66)
- pyqt5designer教程 (65)
- 联想刷bios工具 (66)
- c#源码 (64)
- graphics.h头文件 (62)
- mysqldump下载 (66)
- libmp3lame (60)
- maven3.3.9 (63)
- 二调符号库 (57)
- git.exe下载 (68)
- diskgenius_winpe (72)
- pythoncrc16 (57)
- solidworks宏文件下载 (59)
- qt帮助文档中文版 (73)
- satacontroller (66)
- hgcad (64)
- bootimg.exe (69)
- android-gif-drawable (62)
- axure9元件库免费下载 (57)
- libmysqlclient.so.18 (58)
- springbootdemo (64)
本文暂时没有评论,来添加一个吧(●'◡'●)