在我们开发app时,TextView一定是使用最多的控件了,android自带的TextView的功能也十分强大。但还是有些小的地方不能满足我们的需求。几天要说的这个功能也是开发中非经常见的。就是,在我们显示一段超过屏幕宽度的 String时。TextView会自己主动换行,但系统默认的换行效果是顶起,而不是美工要求的居中。

这时候,就须要我们对系统的TextView做一些改造。已使得换行后文字可以居中显示。

先看下效果图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGNxNTIxMTMxNDEyMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" height="401" width="269">

这样的布局在IOS上非常easy就实现了,android还的自己定义一个View.

思路:在看android.text包中的源代码时。发现几个从来没用到的类。包含:Layout,StaticLayout,DeynamicLayout等几个类。百度后得知这几个类的大概作用:

这三个Layout,就是用来对android的CharSequence及其子类进行布局的,为其传入不同的Alignment,就依照不同的Alignment去处理。代码非常easy,仅仅要从写TextView就可以。代码例如以下:

package com.example.materialdesigndemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView; /**********************************************************
* @文件名:CenterTextView.java
* @文件作者:rzq
* @创建时间:2015年7月2日 上午10:12:16
* @文件描写叙述:换行居中显示TextView
* @改动历史:2015年7月2日创建初始版本号
**********************************************************/
public class CenterTextView extends TextView
{
private StaticLayout myStaticLayout;
private TextPaint tp; public CenterTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
} @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
initView();
} private void initView()
{
tp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
tp.setTextSize(getTextSize());
tp.setColor(getCurrentTextColor());
myStaticLayout = new StaticLayout(getText(), tp, getWidth(), Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
} @Override
protected void onDraw(Canvas canvas)
{
myStaticLayout.draw(canvas);
}
}

使用:     

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="最美的不是下雨天。是和你一起躲过雨的屋檐,漂亮的画面。 " /> <com.example.materialdesigndemo.CenterTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="最美的不是下雨天。是和你一起躲过雨的屋檐,漂亮的画面。" />
</RelativeLayout>

代码非常easy,基本仅仅须要重写onDraw()方法。让StaticLayout的实例去又一次处理一下就可以。这样处理后弊端就是。我们的CenterTextView仅仅能显示文字。无法再显示drawableLeft等,假设须要,就须要在onDraw()方法中进行更复杂的处理。

Demo

android自己定义换行居中CenterTextView的更多相关文章

  1. ANDROID自己定义视图——onLayout源代码 流程 思路具体解释

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 在自己定义view的时候.事实上非常easy.仅仅须要知道3步骤: 1.測量- ...

  2. Android 自己定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...

  3. Android 自己定义View学习(2)

    上一篇学习了基本使用方法,今天学一下略微复杂一点的.先看一下效果图 为了完毕上面的效果还是要用到上一期开头的四步 1,属性应该要有颜色,要有速度 <?xml version="1.0& ...

  4. Android 自己定义ViewGroup 实战篇 -&gt; 实现FlowLayout

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38352503 .本文出自[张鸿洋的博客] 1.概述 上一篇已经基本给大家介绍了怎 ...

  5. Android自己定义DataTimePicker(日期选择器)

    Android自己定义DataTimePicker(日期选择器)  笔者有一段时间没有发表关于Android的文章了,关于Android自己定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中.本篇 ...

  6. Android UI--自定义ListView(实现下拉刷新+加载更多)

    Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...

  7. Android自己定义组件系列【7】——进阶实践(4)

    上一篇<Android自己定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识.这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpa ...

  8. Android 自己定义ScrollView ListView 体验各种纵向滑动的需求

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38950509.本文出自[张鸿洋的博客] 1.概述 群里的一个哥们有个需求是这种: ...

  9. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

随机推荐

  1. 006.MySQL双主-Master02可用配置

    [root@Master02 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_de ...

  2. BZOJ4541 [Hnoi2016]矿区

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  3. zoj 3204 最小生成树,输出字典序最小的解

    注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...

  4. logstash grok 分割匹配日志

    使用logstash的时候,为了更细致的切割日志,会写一些正则表达式. 使用方法 input { file { type => "billin" path => &qu ...

  5. 【汇总】PHP-FPM 配置优化(转)

    -----------------------开启php-fpm慢脚本日志 request_slowlog_timeout = 30sslowlog = /usr/local/php/var/log/ ...

  6. STM32 TIMER OUTPUT DIAGRAM

  7. WCID Devices -- Windows Compatible ID Devices

    WCID Devices What is WCID? A WCID device, where WCID stands for "Windows Compatible ID", i ...

  8. 【stanford C++】容器III——Vector类

    主要介绍如下5个容器类——Vector, Stack,Queue,Map和Set,各个都表示一重要的抽象数据类型.另外,各个类都是一些简单类型的值的集合,所以称它们为容器类. 暂且我们先不需要知道它们 ...

  9. RDMA over TCP的协议栈工作过程浅析

    http://blog.chinaunix.net/uid-140205-id-2849342.html

  10. deeplearningbook-chinese

    https://exacity.github.io/deeplearningbook-chinese/