Android 开发笔记___图像按钮__imageButton
IMAGEBUTTON
其实派生自image view,而不是派生自button。,image view拥有的属性和方法,image button 统统拥有,只是imagebutton有个默认的按钮外观。
- image button 只能显示图形
- imagebutton 上面的图片可按比例拉伸
- 只能在背景显示一张图形,但分别在前景和背景显示两张图片,实现图片叠加的效果
- 在输入法无法输入的字符和特殊字体显示的字符串,就适合用imagebutton, 先切图再显示
- 要想在文字周围放图片可以用基于text view 的 button,
- 具体在xml中设置如下属性:
- drawableTop:指定文本上方的图片
- drawableBottom:指定文本下方的图片
- drawableLeft:指定文本左边的图形
- drawableRight:指定文本右边的图片
- drawablepadding:指定图形文本间距
- 在代码中:
- setCompoundDrawables:设置文本周围图形,上下左右
- setCompoundDrawablePadding:间距

<?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的更多相关文章
- Android 开发笔记___图像视图__简单截屏
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android 开发笔记___图像视图
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android 开发笔记___初级控件之实战__计算器
功能简单,实现并不难,对于初学者可以总和了解初级控件的基本使用. 用到的知识点如下: 线性布局 LinearLayout:整体界面是从上往下的,因此需要垂直方向的linearlayout:下面每行四个 ...
- Android 开发笔记___实战项目:购物车
购物车的应用很广泛,电商app基本上都有它的身影.由于它用到了多种存储方式,通过项目对数据的存储有更高层次的了解. 1.设计思路 首先看看购物车的外观.第一次进入时里面是空的,去购物页面加入购物车以后 ...
- Android 开发笔记___登陆app
package com.example.alimjan.hello_world; /** * Created by alimjan on 7/4/2017. */ import android.con ...
- Android 开发笔记___基本适配器的使用__BaseAdapter
之前用到过ArryAdapter适用于纯文本的列表数据,SimpleAdapter适用于带图标的列表数据,但在实际应用中常常有更复杂的列表,比如同一项中存在多个控件,这时候用前面的两个会比较复杂,而且 ...
- Android 开发笔记___时间选择器---timePicker
像datepicker一样,也有timepicker. 同样有timepickerdialog 所用到的方法还是一样,监听时间选择器的变化. package com.example.alimjan.h ...
- Android 开发笔记___存储方式__共享参数__sharedprefences
Android 的数据存储方式有四种,这次是[共享参数__sharedprefences] 听起来挺别扭的,平时看到的app里面,当用户删除了一些软件以后下次安装,发现原来的设置还在,这种情况就是把一 ...
- Android 开发笔记___复选框__checkbox
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
随机推荐
- escape()、encodeURI()、encodeURIComponent()区别详解(转)
JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,dec ...
- 【Python练习1】统计一串字符中英文字母、空格、数字和其他字符的个数
练习思路: 1.输入一串字符 2.筛选出字符中的英文字母并统计 3.筛选出字符中的空格并统计 4.筛选出字符中的数字并统计 5.筛选出字符中的其他字符并统计 代码实现: def msg(s): abc ...
- yum软件管理器,及yum源配置
说到yum源就必须说到linux系统中特有的依赖关系问题,yum就是为了解决依赖关系而存在的.yum源就相当是一个目录项,当我们使用yum机制安装软件时,若需要安装依赖软件,则yum机制就会根据在yu ...
- hdu 4090--GemAnd Prince(搜索)
题目链接 Problem Description Nowadays princess Claire wants one more guard and posts the ads throughout ...
- Python操作csv文件
1.什么是csv文件 The so-called CSV (Comma Separated Values) format is the most common import and export fo ...
- 在CentOS6上编译安装实现LAMP(php-modules)+phpMyAdmin安装过程全记录
php与apache协作有三种模式:CGI.modules.FastCGI. 在CGI模式下,用户请求php文件时,apache会启动响应进程,调用php处理器处理请求,然后将结果返回给客户端.用户响 ...
- 从头编写 asp.net core 2.0 web api 基础框架 (2)
上一篇是: http://www.cnblogs.com/cgzl/p/7637250.html Github源码地址是: https://github.com/solenovex/Building- ...
- 想到一个赚钱的APP
通过APP上发布调查问卷的需求,鼓励人们注册,并给与一定的报酬.需求主要面向一些调查问卷,一类的需求发布
- 在项目中创建单元测试时junit的配置和使用
首先配置项目中AndroidMainfest.xml文件,加入 <instrumentation android:name="android.test.InstrumentationT ...
- Python之scrapy实例1
下文参考:http://www.jb51.net/article/57183.htm 个人也是稍加整理,修改其中的一些错误,这些错误与scrapy版本选择有关,个环境:Win7x64_SP1 + Py ...