在我们的项目中,常常会碰到图片与文字混排的问题。解决这类问题的方法有非常多,本文给出的方法不是唯一的。仅仅有依据实际场景才干找到更适合的方法。

本文主要通过xml布局来实现图片与文字的混排(水平排列)。

1.利用TextView实现图片与文字混排,

android:drawableBottom在text的下方输出一个drawable。如图片。

假设指定一个颜色的话会把text的背景设为该颜色。而且同一时候和background使用时覆盖后者。

android:drawableLeft在text的左边输出一个drawable,如图片。

android:drawablePadding设置text与drawable(图片)的间隔,

与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数。单独使用没有效果。

android:drawableRight在text的右边输出一个drawable。

android:drawableTop在text的正上方输出一个drawable。

<span style="font-size:18px;">        <TextView
android:id="@+id/my_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在线"
android:textColor="#85898f"
android:layout_marginTop="5dp"
android:drawablePadding="5dp"
android:drawableLeft="@drawable/user_online"/></span>

当中, android:drawablePaddingh非常好的攻克了图片与文字的间距问题。

2.TextView动态的设置图片

Drawable drawable= context.getResources().getDrawable(R.drawable.text_img);
// 调用setCompoundDrawables时。必须调用Drawable.setBounds()方法,否则图片不显示
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
textView.setCompoundDrawables(drawable, null, null, null); //设置左图标

3.利用RelativeLayout(LinearLayout) 加入 TextView 和 ImageView(ButtonView)来实现

<span style="font-size:18px;">   <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <ImageView
android:id="@+id/my_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/user_online"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/> <TextView
android:id="@+id/my_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/my_iv"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
</RelativeLayout></span>

事实上也能够通过java代码来实现图片和文字的混排。

Android 实现文字与图片的混排的更多相关文章

  1. android带有文字的图片按钮的两种实现方式

    android带有文字的图片按钮的两种实现方式 1). TextView对Button用相对布局,这要要求按钮的背景图片要留下空白位置给文字.这种方式开发比较简单,适合做一些风格一致的Button. ...

  2. android:怎样在TextView实现图文混排

    我们通常在TextView文本中设置文字.但是怎样设置图文混排呢? 我就在这里写一个样例 .我们须要用到一点简单的HTML知识 在TextView中预订了一些类似HTML的标签,通过标签能够使Text ...

  3. Android 带文字的图片分享

    这里也记录下上下文,因为做了一个失物招领的App,当有人上交了失物之后,可以将这个消息分享出去,这个消息内容有物品的信息和图片,而微信SDK始终无法做到,就想着把物品信息嵌入到图片中分享出去,先放一个 ...

  4. Android中Textview显示Html,图文混排,支持图片点击放大

    本文首发于网易云社区 对于呈现Html文本来说,Android提供的Webview控件可以得到很好的效果,但使用Webview控件的弊端是效率相对比较低,对于呈现简单的html文本的话,杀鸡不必使用牛 ...

  5. javaWeb css图文混排

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. [UGUI]图文混排(一):标签制定和解析

    参考链接: https://github.com/SylarLi/RichText/tree/master/Assets/Scripts 正则表达式: https://blog.csdn.net/ly ...

  7. Android 图片混排富文本编辑器控件

    概述 一个Android 图片混排富文本编辑器控件(仿兴趣部落) 详细 代码下载:http://www.demodashi.com/demo/12032.html 一.一个Android 图片混排富文 ...

  8. Android自动解析html带图片,实现图文混排

    在android中,如何将html代码转换为text,然后显示在textview中呢,有一个简单直接的方法: textView.setText(Html.fromHtml(content)); 然而用 ...

  9. 图片文字混排的垂直居中、inline-block块元素和行内元素混排的垂直居中问题

    图片.文字混排: 不管图片和文字的前后位置,都要给 图片 设置 vertical-algin,而不是谁在前面给谁设置. 此方法兼容IE7+ 和其它主流浏览器.IE7-没有测. inline-block ...

随机推荐

  1. Python随笔-函数

    在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. def GetMax(v1, v2):#注 ...

  2. iOS- Size Class使用教程

    1:它引入了一种新的概念,抛弃传统意义上我们适配时所谓的具体宽高尺寸,把屏幕的宽和高分别分成两种情况:Compact-紧凑, Regular-正常(Any-任意,其实就是这2种的组合,所以我没分成3种 ...

  3. linux下用scp命令在两个服务器之间传输文件,利用php_scp函数进行文件传输

    在linux下利用scp进行文件传输, 从服务器下载文件 scp username@servername:/path/filename /path/filename 上传本地文件到服务器 scp /p ...

  4. Node.js概述

    Node.js最重要的特性:通过单线程实现异步处理环境 Node.js解决的问题: Node.js修改了客户端连接服务器端的连接方法,不需要为每个客户端连接创建一个新的线程,而是为每个客户端连接触发一 ...

  5. Codeforces_733C

    C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. CAD当前选择实体发生变化调用事件(com接口)

    主要用到函数说明: _DMxDrawXEvents::SelectModified 当前选择实体发生变化,会调用该事件,详细说明如下: 参数 说明 IDispatch* pAryId 当前被选择的实体 ...

  7. 28 I/O限制的异步操作

    28.2 C#的异步函数 private static async Task<string> IssueClientRequestAsync(string serverName, stri ...

  8. unigui的session【1】

    目前是1394. 明白session如何使用管理,看demo Session List和SessionTimeout unit Main; interface uses Windows, Messag ...

  9. 【codeforces 515A】Drazil and Date

    [题目链接]:http://codeforces.com/contest/515/problem/A [题意] 每次只能走到相邻的四个格子中的一个; 告诉你最后走到了(a,b)走了多少步->s ...

  10. 【codeforces 764D】Timofey and rectangles

    [题目链接]:http://codeforces.com/contest/764/problem/D [题意] 给你n个矩形,以左下角坐标和右上角坐标的形式给出; (保证矩形的边长为奇数) 问你有没有 ...