每日一练之Android-ImageView

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







如果需要同时展示文本与图像

两种方式

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);//只放左边

}

布局

展开阅读全文

页面更新:2024-04-11

标签:控件   图标   布局   按钮   属性   图像   文本   背景   文字   图片

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top