Android 开发笔记___图像按钮__imageButton

时间:2023-01-10 18:29:08

IMAGEBUTTON

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

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

Android 开发笔记___图像按钮__imageButton

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:drawableLeft="@mipmap/ic_launcher"
android:drawablePadding="5dp"
android:text="热烈欢迎"
android:textColor="#000000"
android:textSize="17sp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"> <Button
android:id="@+id/btn_left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在左"
android:textColor="#000000"
android:textSize="15sp" /> <Button
android:id="@+id/btn_top"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在上"
android:textColor="#000000"
android:textSize="15sp" /> <Button
android:id="@+id/btn_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在右"
android:textColor="#000000"
android:textSize="15sp" /> <Button
android:id="@+id/btn_bottom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在下"
android:textColor="#000000"
android:textSize="15sp" /> </LinearLayout> </LinearLayout>

java

 package com.example.alimjan.hello_world;

 import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button; /**
* Created by alimjan on 7/1/2017.
*/ public class class__2_3_4 extends AppCompatActivity implements View.OnClickListener {
private Button btn_icon;
private Drawable drawable; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_2_3_4);
btn_icon = (Button) findViewById(R.id.btn_icon);
drawable = getResources().getDrawable(R.mipmap.ic_launcher);
// 必须设置图片大小,否则不显示图片
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
Button btn_left = (Button) findViewById(R.id.btn_left);
Button btn_top = (Button) findViewById(R.id.btn_top);
Button btn_right = (Button) findViewById(R.id.btn_right);
Button btn_bottom = (Button) findViewById(R.id.btn_bottom);
btn_left.setOnClickListener(this);
btn_top.setOnClickListener(this);
btn_right.setOnClickListener(this);
btn_bottom.setOnClickListener(this);
} @Override
public void onClick(View v) {
if (v.getId() == R.id.btn_left) {
btn_icon.setCompoundDrawables(drawable, null, null, null);
} else if (v.getId() == R.id.btn_top) {
btn_icon.setCompoundDrawables(null, drawable, null, null);
} else if (v.getId() == R.id.btn_right) {
btn_icon.setCompoundDrawables(null, null, drawable, null);
} else if (v.getId() == R.id.btn_bottom) {
btn_icon.setCompoundDrawables(null, null, null, drawable);
}
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class__2_3_4.class);
mContext.startActivity(intent);
}
}

Android 开发笔记___图像按钮__imageButton的更多相关文章

  1. Android 开发笔记&lowbar;&lowbar;&lowbar;图像视图&lowbar;&lowbar;简单截屏

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. Android 开发笔记&lowbar;&lowbar;&lowbar;图像视图

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  3. Android 开发笔记&lowbar;&lowbar;&lowbar;初级控件之实战&lowbar;&lowbar;计算器

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

  4. Android 开发笔记&lowbar;&lowbar;&lowbar;实战项目:购物车

    购物车的应用很广泛,电商app基本上都有它的身影.由于它用到了多种存储方式,通过项目对数据的存储有更高层次的了解. 1.设计思路 首先看看购物车的外观.第一次进入时里面是空的,去购物页面加入购物车以后 ...

  5. Android 开发笔记&lowbar;&lowbar;&lowbar;登陆app

    package com.example.alimjan.hello_world; /** * Created by alimjan on 7/4/2017. */ import android.con ...

  6. Android 开发笔记&lowbar;&lowbar;&lowbar;基本适配器的使用&lowbar;&lowbar;BaseAdapter

    之前用到过ArryAdapter适用于纯文本的列表数据,SimpleAdapter适用于带图标的列表数据,但在实际应用中常常有更复杂的列表,比如同一项中存在多个控件,这时候用前面的两个会比较复杂,而且 ...

  7. Android 开发笔记&lowbar;&lowbar;&lowbar;时间选择器---timePicker

    像datepicker一样,也有timepicker. 同样有timepickerdialog 所用到的方法还是一样,监听时间选择器的变化. package com.example.alimjan.h ...

  8. Android 开发笔记&lowbar;&lowbar;&lowbar;存储方式&lowbar;&lowbar;共享参数&lowbar;&lowbar;sharedprefences

    Android 的数据存储方式有四种,这次是[共享参数__sharedprefences] 听起来挺别扭的,平时看到的app里面,当用户删除了一些软件以后下次安装,发现原来的设置还在,这种情况就是把一 ...

  9. Android 开发笔记&lowbar;&lowbar;&lowbar;复选框&lowbar;&lowbar;checkbox

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

随机推荐

  1. &lbrace;POJ&rcub;&lbrace;3971&rcub;&lbrace;Scales&rcub;&lbrace;O&lpar;N&rpar;动态规划&rcub;

    题意:给定一堆2二进制砝码,给定一个物品,要求在天平两端加入物品和砝码使之平衡,求可能数. 思路:一开始想到了直接用数学原理,结果没证出来.做如下思考,此题需要用二进制: (1)设物品重量为w,加入的 ...

  2. Ubuntu&sol;Windows双系统修复引导

    Ubuntu/Windows双系统修复引导   首先说明:在Windows存在的前提下安装Ubuntu(或者Ubuntu系列)是不需要修复引导的.因为grub会自动搜索存在硬盘中的系统.   而在Ub ...

  3. 转:最简单的视频网站(JavaEE&plus;FFmpeg)

    本文记录一个最简单的视频网站系统.此前做过一些基于JavaEE中的 SSH (Strut2 + Spring + Hibernate)的网站系统,但是一直没有做过一个视频网站系统,所以就打算做一个&q ...

  4. poj 3249 Test for Job (DAG最长路 记忆化搜索解决)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8990   Accepted: 2004 Desc ...

  5. JQuery window、document、 body

    我电脑屏幕分辨率:1440 * 900   最大化浏览器,刷新浏览器 alert($(window).width() + "---" + $(window).height()); ...

  6. js中console使用2

    接着上一篇js中console使用1,本片继续介绍js中console的用法 测试代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 ...

  7. 学习笔记&colon; yield迭代器

    yield 与 IEnumerable<T> 结对出现, 可实现按需获取 , 迭代器模式 static void Main(string[] args)         {         ...

  8. vscode 设置 cmder为终端

    "terminal.integrated.shell.windows": "cmd.exe", "terminal.integrated.shellA ...

  9. JS --- trim&lpar;&rpar; 函数

    trim()是一个很适用的方法,作用是去除字符串两边的空白,但是js本身并未提供这个方法,下面介绍js使用trim()的方法. 1.通过原型创建字符串的trim() //去除字符串两边的空白 Stri ...

  10. LIBSVM使用方法及参数设置 主要参考了一些博客以及自己使用经验。

    主要参考了一些博客以及自己使用经验.收集来觉得比较有用的. LIBSVM 数据格式需要---------------------- 决策属性  条件属性a  条件属性b  ... 2    1:7   ...