前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ImageButton和ZoomButton使用

ImageButton和ZoomButton使用

作者头像
李小白是一只喵
发布2020-04-24 08:43:42
9600
发布2020-04-24 08:43:42
举报
文章被收录于专栏:算法微时光

image.png

目录

ImageButton(图像按钮)

Android开发中除了使用Button按钮,还可以使用自带图标的按钮,即ImageButton。

Button与ImageButton的区别在于,Button生成的按钮上显示文字,而ImageButton上则显示图片。

注意 为ImageButton按钮指定android:text属性没用,由于ImageButton的本质是ImageView,即使指定了该属性,图片按钮上也不会显示任何文字。

使用方式:

代码语言:javascript
复制
    <ImageButton
        android:id="@+id/recentralization"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|bottom"
        android:src="@drawable/p002"/>

实例

下面使用ImageButton实现一个点击后切换图片的小例子: xml文件:

代码语言:javascript
复制
<?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"
    tools:context=".MainActivity">


    <ImageButton
        android:id="@+id/imagebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|bottom"
        android:src="@drawable/p002"/>

</android.support.constraint.ConstraintLayout>

注意:图片需要自己导入到资源文件夹中才可以使用.

代码:

代码语言:javascript
复制
package com.example.user.imagetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {
    ImageButton imagebtn;
    int i = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取图片按钮
        imagebtn = (ImageButton)findViewById(R.id.imagebtn);
        // 设置点击函数
        imagebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                i++;
                if(i%2 != 0)
                    // 重新设定图片资源
                    imagebtn.setImageResource(R.drawable.p003);
                else
                    imagebtn.setImageResource(R.drawable.p002);
            }
        });
    }
}

运行效果

image.png

image.png

ZoomButton(缩放按钮)

ZoomButton是ImageButton派生的一个类,ZoomButton可以代表“放大”、“缩小”两个按钮。

ZoomButton 基本类似于 ImageButton,只是 Android 默认提供了 btn_minus、btn_plus 两个 Drawable 资源。当然也可以自己指定图片资源。

使用方式

代码语言:javascript
复制
    <ZoomButton
        android:id="@+id/btn_minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/btn_minus" />

使用效果

image.png

参考

ImageView子控件,ImageButton和ZoomButton使用

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • ImageButton(图像按钮)
  • 实例
  • 运行效果
  • ZoomButton(缩放按钮)
  • 使用效果
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档