欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android DrawableTextView圖片文字居中顯示實(shí)例

 更新時(shí)間:2017年03月01日 09:02:39   作者:xing-java  
在我們開(kāi)發(fā)中,TextView設(shè)置Android:drawableLeft一定使用的非常多,但Drawable和Text同時(shí)居中顯示可能不好控制,小編想到通過(guò)自定義TextView實(shí)現(xiàn),具體詳情大家參考下本文

 在我們開(kāi)發(fā)中,TextView設(shè)置Android:drawableLeft一定使用的非常多,但Drawable和Text同時(shí)居中顯示可能不好控制,有沒(méi)有好的辦法解決呢?

小編的方案是通過(guò)自定義TextView實(shí)現(xiàn)。

實(shí)現(xiàn)的效果圖:

這里寫(xiě)圖片描述

注:第一行為原生TextView添加android:drawableLeft

第二行為自定義TextView實(shí)現(xiàn)的效果。

實(shí)現(xiàn)思路:

繼承TextView,覆蓋onDraw(Canvas canvas),在onDraw中先將canvas進(jìn)行translate平移,再調(diào)用父類onDraw進(jìn)行繪制。

DrawableTextView.Java:
package com.xing.drawabletextview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;
/**
 * Created by Administrator on 2017/2/28.
 */
public class DrawableTextView extends TextView {
  public DrawableTextView(Context context) {
    this(context, null);
  }
  public DrawableTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
  public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }
  @Override
  protected void onDraw(Canvas canvas) {
    // getCompoundDrawables() : Returns drawables for the left, top, right, and bottom borders.
    Drawable[] drawables = getCompoundDrawables();
    // 得到drawableLeft設(shè)置的drawable對(duì)象
    Drawable leftDrawable = drawables[0];
    if (leftDrawable != null) {
      // 得到leftDrawable的寬度
      int leftDrawableWidth = leftDrawable.getIntrinsicWidth();
      // 得到drawable與text之間的間距
      int drawablePadding = getCompoundDrawablePadding();
      // 得到文本的寬度
      int textWidth = (int) getPaint().measureText(getText().toString().trim());
      int bodyWidth = leftDrawableWidth + drawablePadding + textWidth;
      canvas.save();
      canvas.translate((getWidth() - bodyWidth) / 2, 0);
    }
    super.onDraw(canvas);
  }
}

布局文件中引入:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal">
    <com.xing.drawabletextview.DrawableTextView
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:drawableLeft="@drawable/ic_one"
      android:drawablePadding="10dp"
      android:gravity="center_vertical"
      android:text="21" />
    <com.xing.drawabletextview.DrawableTextView
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:drawableLeft="@drawable/ic_two"
      android:drawablePadding="10dp"
      android:gravity="center_vertical"
      android:text="99" />
    <com.xing.drawabletextview.DrawableTextView
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:drawableLeft="@drawable/ic_three"
      android:drawablePadding="10dp"
      android:gravity="center_vertical"
      android:text="99+" />
  </LinearLayout>

以上所述是小編給大家介紹的Android DrawableTextView圖片文字居中顯示實(shí)例,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android View事件分發(fā)機(jī)制詳解

    Android View事件分發(fā)機(jī)制詳解

    這篇文章主要為大家詳細(xì)介紹了Android View事件分發(fā)機(jī)制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android中截取當(dāng)前屏幕圖片的實(shí)例代碼

    Android中截取當(dāng)前屏幕圖片的實(shí)例代碼

    該篇文章是說(shuō)明在Android手機(jī)或平板電腦中如何實(shí)現(xiàn)截取當(dāng)前屏幕的功能,并把截取的屏幕保存到SDCard中的某個(gè)目錄文件夾下面。實(shí)現(xiàn)的代碼如下:
    2013-08-08
  • Android TextView對(duì)齊的兩種方法

    Android TextView對(duì)齊的兩種方法

    這篇文章主要介紹了Android TextView對(duì)齊的兩種方法的相關(guān)資料,在開(kāi)發(fā)Android APP 的時(shí)候經(jīng)常會(huì)用到TextView 輸入用戶信息或者其他信息,總是不能對(duì)齊,這里提供兩種方法,需要的朋友可以參考下
    2017-07-07
  • Android Call(打電話)的基本知識(shí)詳解

    Android Call(打電話)的基本知識(shí)詳解

    本文主要介紹了Android Call(打電話)的基本知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • 源碼分析Android?rinflate的使用

    源碼分析Android?rinflate的使用

    這篇文章主要將從源碼的角度帶大家一起分析Android?rinflate的使用,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下
    2023-04-04
  • Android自定義Dialog內(nèi)部透明、外部遮罩效果

    Android自定義Dialog內(nèi)部透明、外部遮罩效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義Dialog內(nèi)部透明、外部遮罩效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android調(diào)用堆棧跟蹤實(shí)例分析

    Android調(diào)用堆棧跟蹤實(shí)例分析

    這篇文章主要介紹了Android調(diào)用堆棧跟蹤的方法,以實(shí)例形式較為詳細(xì)的分析了Android錯(cuò)誤信息分析的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android調(diào)用第三方QQ登錄代碼分享

    Android調(diào)用第三方QQ登錄代碼分享

    現(xiàn)在的項(xiàng)目開(kāi)發(fā),調(diào)用第三方登錄,幾乎是必須的,這篇文章主要介紹了Android調(diào)用第三方QQ登錄代碼分享
    2016-05-05
  • Android仿微信微博多圖展示效果

    Android仿微信微博多圖展示效果

    這篇文章主要為大家詳細(xì)介紹了Android仿微信微博多圖展示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼)

    Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼)

    這篇文章主要介紹了Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02

最新評(píng)論