Android控件View的文字周围添加图标
作者:飞哥来了 发布时间:2023-02-20 04:04:44
标签:Android,图标
在Android控件View的文字周围添加图标,供大家参考,具体内容如下
在控件TextView文字周围放置图片(基于TextView的Button也能实现),减少多布局组合嵌套。
优点:使用LinearLayout对ImageView和TextView组合布局固然可行, 但是布局文件会冗长许多。
以TextView为例:
在XML布局文件中设置以下5个属性:
drawableTop: 指定文本上方的图形。
drawableBottom: 指定文本下方的图形。
drawableLeft: 指定文本左边的图形。
drawableRight: 指定文本右边的图形。
drawablePadding: 指定图形与文本的间距。
若在代码中实现, 则可调用如下方法。
etCompoundDrawables: 设置文本周围的图形。 可分别设置左边、 上边、 右边、 下边的图形。
setCompoundDrawablePadding: 设置图形与文本的间距。
setBounds: 设置图形对象的矩形边界大小,必须设置图片大小,否则不会显示图片。
:
运行效果图:
示例代码如下:
public class IconActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_icon; // 声明一个按钮对象
private Drawable drawable; // 声明一个图形对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon);
// 从布局文件中获取名叫btn_icon的按钮控件
btn_icon = findViewById(R.id.btn_icon);
// 从资源文件ic_launcher.png中获取图形对象
drawable = getResources().getDrawable(R.drawable.ic_smile);
// 设置图形对象的矩形边界大小,注意必须设置图片大小,否则不会显示图片
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
// 通过四个按钮分别演示:左、上、右、下四个方向展示图标的效果
findViewById(R.id.btn_left).setOnClickListener(this);
findViewById(R.id.btn_top).setOnClickListener(this);
findViewById(R.id.btn_right).setOnClickListener(this);
findViewById(R.id.btn_bottom).setOnClickListener(this);
}
@Override
public void onClick(View v) { // 一旦监听到点击动作,就触发 * 的onClick方法
// 监听到点击动作,就触发 * 的onClick方法
switch (v.getId()) {
case R.id.btn_left:
// 设置按钮控件btn_icon内部文字左边的图标
btn_icon.setCompoundDrawables(drawable, null, null, null);
break;
case R.id.btn_top:
// 设置按钮控件btn_icon内部文字上方的图标
btn_icon.setCompoundDrawables(null, drawable, null, null);
break;
case R.id.btn_right:
// 设置按钮控件btn_icon内部文字右边的图标
btn_icon.setCompoundDrawables(null, null, drawable, null);
break;
case R.id.btn_bottom:
// 设置按钮控件btn_icon内部文字下方的图标
btn_icon.setCompoundDrawables(null, null, null, drawable);
break;
default:
}
}
xml中设置的2行核心代码
//在控件左侧设置图标
android:drawableLeft="@drawable/ic_smile"
//设置图标与控件文件的间距
android:drawablePadding="10dp"
布局xml文件
<?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:padding="12dp"
android:orientation="vertical">
<Button
android:id="@+id/btn_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:drawableLeft="@drawable/ic_smile"
android:drawablePadding="10dp"
android:text="热烈欢迎"
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:textSize="15sp" />
<Button
android:id="@+id/btn_top"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在上"
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:textSize="15sp" />
<Button
android:id="@+id/btn_bottom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="图标在下"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
来源:https://blog.csdn.net/qianfeifeio/article/details/115303219
0
投稿
猜你喜欢
- public class TimeUtil { public static final int SECO
- 首先看一看什么是装箱和拆箱?简单的来说:装箱就是值类型转换为引用类型;拆箱就是引用类型转换为值类型。值类型,包括原类型(Sbyte、Byte
- 首先,必须要强调的一点,MD5不是加密算法,而是消息摘要算法,具有不可逆性。字符串通过MD5处理后会生成128位的二进制串。我们通常会将其转
- 本文实例讲述了C#数字图像处理之图像缩放的方法。分享给大家供大家参考。具体如下://定义图像缩放函数private static Bitma
- 最近项目需要微信支付,然后看了下微信公众号支付,,虽然不难,但是细节还是需要注意的,用了大半天时间写了个demo,并且完整的测试了一下支付流
- 导入maven项目各个注解均报错所遇问题导入maven项目各个注解均报错了思考1:这个项目使用了springboot;spring是个”大容
- 1.相关概念Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。建
- WebView 网页滚动截屏,可对整个网页进行截屏而不是仅当前屏幕哦! 注意若Web页面存在position:fixed; 的话得在调用前设
- 下面就来分享工具类的内容:使用范围:JavaBean类对象的属性不能是数组、List、Set、Mappublic class MapBean
- 本文实例为大家分享了C#线程中弹窗的制作代码,供大家参考,具体内容如下首先建立一个ShowFrom窗体,窗体中放入两个按钮分别为确定和取消分
- 最近重构了一下我的存档框架。我在这里对实现方法进行简单的解析。注意这里主要演示算法,所以,效率上并不是最佳。一个游戏中,可能有成百上千个物体
- 直接调用HashKit.sha1(String str)方法就可以了,,返回的是16进制的字符串长度是40,也就是用md.digest()方
- 前言在介绍Dubbo之前先了解一下基本概念:Dubbo是一个RPC框架,RPC,即Remote Procedure Call(远程过程调用)
- 接口定义了一系列的行为规范,为类型定义一种Can-Do的功能。例如,实现IEnumerable接口定义了GetEnumerator方法,用于
- 大家都知道在C#里面,我们可以使用Thread.Start方法来启动一个线程,当我们想停止执行的线程时可以使用Thread.Abort方法来
- 在 Java 中将 Object 转换为 Int我们可以使用 Object 类来引用我们在 Java 中不知道其类型的任
- 1. 实现原理将key映射到 2^32 - 1 的空间中,将这个数字的首尾相连,形成一个环计算节点(使用节点名称、编号、IP地址)的hash
- 首先 下载 jedis.jar包然后再 工程设置里面找到Libraries,点击+。添加下载好的jedis.jar包。点击OK退出即可创建J
- 有一天,你写了好多好多带“形参”的构造函数(就是“方法”,同义),而且需要向这些构造函数里传递同样的“实参”,然后你就憨憨地一个一个函数的调
- 长久以来统领javaee领域的脚手架以spring struts2 mybatis/hib