IMAGEBUTTON

其实派生自image view,而不是派生自button。,image view拥有的属性和方法,image button 统统拥有,只是imagebutton有个默认的按钮外观。

  • image button  只能显示图形
  • imagebutton 上面的图片可按比例拉伸
  • 只能在背景显示一张图形,但分别在前景和背景显示两张图片,实现图片叠加的效果
  • 在输入法无法输入的字符和特殊字体显示的字符串,就适合用imagebutton,   先切图再显示
  • 要想在文字周围放图片可以用基于text view 的 button,
  • 具体在xml中设置如下属性:
    • drawableTop:指定文本上方的图片
    • drawableBottom:指定文本下方的图片
    • drawableLeft:指定文本左边的图形  
    • drawableRight:指定文本右边的图片
    • drawablepadding:指定图形文本间距
  • 在代码中:
    • setCompoundDrawables:设置文本周围图形,上下左右
    • setCompoundDrawablePadding:间距

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6
 7     <Button
 8         android:id="@+id/btn_icon"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_marginTop="10dp"
12         android:layout_gravity="center"
13         android:drawableLeft="@mipmap/ic_launcher"
14         android:drawablePadding="5dp"
15         android:text="热烈欢迎"
16         android:textColor="#000000"
17         android:textSize="17sp" />
18
19     <LinearLayout
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:layout_marginTop="10dp"
23         android:orientation="horizontal">
24
25         <Button
26             android:id="@+id/btn_left"
27             android:layout_width="0dp"
28             android:layout_height="wrap_content"
29             android:layout_weight="1"
30             android:text="图标在左"
31             android:textColor="#000000"
32             android:textSize="15sp" />
33
34         <Button
35             android:id="@+id/btn_top"
36             android:layout_width="0dp"
37             android:layout_height="wrap_content"
38             android:layout_weight="1"
39             android:text="图标在上"
40             android:textColor="#000000"
41             android:textSize="15sp" />
42
43         <Button
44             android:id="@+id/btn_right"
45             android:layout_width="0dp"
46             android:layout_height="wrap_content"
47             android:layout_weight="1"
48             android:text="图标在右"
49             android:textColor="#000000"
50             android:textSize="15sp" />
51
52         <Button
53             android:id="@+id/btn_bottom"
54             android:layout_width="0dp"
55             android:layout_height="wrap_content"
56             android:layout_weight="1"
57             android:text="图标在下"
58             android:textColor="#000000"
59             android:textSize="15sp" />
60
61     </LinearLayout>
62
63 </LinearLayout>

java

 1 package com.example.alimjan.hello_world;
 2
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.graphics.drawable.Drawable;
 6 import android.os.Bundle;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.view.View;
 9 import android.widget.Button;
10
11 /**
12  * Created by alimjan on 7/1/2017.
13  */
14
15
16 public class class__2_3_4 extends AppCompatActivity implements View.OnClickListener {
17     private Button btn_icon;
18     private Drawable drawable;
19
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.code_2_3_4);
24         btn_icon = (Button) findViewById(R.id.btn_icon);
25         drawable = getResources().getDrawable(R.mipmap.ic_launcher);
26         // 必须设置图片大小,否则不显示图片
27         drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
28         Button btn_left = (Button) findViewById(R.id.btn_left);
29         Button btn_top = (Button) findViewById(R.id.btn_top);
30         Button btn_right = (Button) findViewById(R.id.btn_right);
31         Button btn_bottom = (Button) findViewById(R.id.btn_bottom);
32         btn_left.setOnClickListener(this);
33         btn_top.setOnClickListener(this);
34         btn_right.setOnClickListener(this);
35         btn_bottom.setOnClickListener(this);
36     }
37
38     @Override
39     public void onClick(View v) {
40         if (v.getId() == R.id.btn_left) {
41             btn_icon.setCompoundDrawables(drawable, null, null, null);
42         } else if (v.getId() == R.id.btn_top) {
43             btn_icon.setCompoundDrawables(null, drawable, null, null);
44         } else if (v.getId() == R.id.btn_right) {
45             btn_icon.setCompoundDrawables(null, null, drawable, null);
46         } else if (v.getId() == R.id.btn_bottom) {
47             btn_icon.setCompoundDrawables(null, null, null, drawable);
48         }
49     }
50
51     public static void startHome(Context mContext) {
52         Intent intent = new Intent(mContext, class__2_3_4.class);
53         mContext.startActivity(intent);
54     }
55 }

转载于:https://www.cnblogs.com/alimjan/p/7102001.html

Android 开发笔记___图像按钮__imageButton相关推荐

  1. Android 开发笔记___初级控件之实战__计算器

    功能简单,实现并不难,对于初学者可以总和了解初级控件的基本使用. 用到的知识点如下: 线性布局 LinearLayout:整体界面是从上往下的,因此需要垂直方向的linearlayout:下面每行四个 ...

  2. Android开发笔记(三十七)按钮类控件

    Button与ImageButton Button是文本按钮(继承自TextView),而ImageButton是图像按钮(继承自ImageView).两者之间的区别在于: 1.Button即可显示文 ...

  3. Android开发笔记(一百七十四)图像解码器ImageDecoder

    早期的Android只支持三种图像格式,分别是JPEG.PNG和GIF,虽然这三类图片都能在ImageView上显示,但对于GIF格式来说,图像视图仅能显示动图的初始画面,无法直接播放动画效果.此外, ...

  4. Android开发笔记(序)写在前面的目录

    知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经验教训,与网友互相切磋,从而去芜存菁进一步提升自己的水平.因此博主就想,入门的东西咱就不写了,人不能老停留在入 ...

  5. Android开发笔记(一百六十九)利用BottomNavigationView实现底部标签栏

    在Android Studio上创建官方默认的首屏标签页面很方便,首先右击需要添加标签栏的模块,在弹出的右键菜单中依次选择"New"--"Activity"-- ...

  6. Android开发笔记(一百三十七)自定义行为Behavior

    协调布局CoordinatorLayout能够让内部的子控件互相配合着移动,这跟以往孤立的控件运动截然不同,协调运动的结果,意味着App画面更加流畅.自然,也更符合日常生活中的动作场景.如果你对Coo ...

  7. Android开发笔记(一百三十四)协调布局CoordinatorLayout

    协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升,一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...

  8. Android开发笔记(一百三十)截图和录屏

    屏幕捕捉 Android5.0之后开放了屏幕捕捉的API,因此开发者便可以直接通过代码进行截图与录屏,而无需操作系统底层了.屏幕捕捉的功能由MediaProjectionManager媒体投影管理器实 ...

  9. Android开发笔记(一百零五)社会化分享SDK

    社会化分享 社会化分享,指的是用户通过互联网这个媒介,把文本/图片/多媒体信息分享到该用户的交际圈,从而加快信息传播的行为.对于app来说,网络社区虽多,但用户量足够大的就那几个,所以app的社会化分 ...

  10. Android开发笔记(二十)顶部导航栏ActionBar

    标题栏ActionBar ActionBar是在Android3.0之后引入的,所以Android2.x之前的版本不能直接使用ActionBar.现在ActionBar广泛用做APP的顶部导航栏,它在 ...

最新文章

  1. 静态网页案例_手把手教你构建网页
  2. 2、以太网基础知识——IP协议
  3. python怎么由列表生成m*n的矩阵_Python进阶系列:Python遍历的秘密
  4. 大文件下载及视频点播的CDN加速实践
  5. CF 335B. Palindrome(DP)
  6. 第十一届蓝桥杯省赛C++组试题 第6题
  7. Hyper-v副本容量规划器
  8. FR帧中继(点对点子接口)
  9. 浅谈 标准的代号和编号
  10. python基础系统性学习
  11. Git之路——配置SSH免密码登录
  12. Flink中的时间和窗口 完整使用 (第六章)
  13. 网站支付宝登录授权具体实现
  14. Inkscape如何将png图片转换为svg图片并且不失真
  15. 中兴服务器400,中兴通讯高性能400G方案:攀登5G高峰
  16. 国家自然基金上会评审若干情况介绍
  17. 采油厂污水选用流量计的类型建议
  18. pip install mysqlclient安装
  19. 科普系列—低速容错CAN的容错机制介绍
  20. Python+Tkinter画图工具

热门文章

  1. MT5 EA交易期货-获得持仓
  2. 【网页设计】31:文本声明
  3. 【PHP编程】WebCat,手机搭建PHP运行环境及项目运行
  4. onedrive直链_OneDrive直链下载OneIndex
  5. PR值计算公式带来的思考
  6. SCI-HUB最新文献网站
  7. 网页超链接无法访问本地文件
  8. 草稿 22.10.10 !!!!!! YUYU
  9. 33暴力破解(MD5撞击)
  10. vsftpd虚拟账户(虚拟用户,ubuntu16,舍弃虚拟用户配置文件)