Android 優(yōu)化布局(解決TextView drawableLeft/top/right布局中大小不可控的方法)

今天分享一個(gè)Layout布局中的一個(gè)小技巧贿衍,希望看過(guò)之后你也可以寫出性能更好的布局哺窄,我個(gè)人的目的是用最少的view寫出一樣的效果布局

用TextView同時(shí)顯示圖片和文字:

先看一下效果圖

圖像 3.png
以上這四塊區(qū)域相信大家在項(xiàng)目中經(jīng)常遇到吧U咛睢(一般的寫法ImageView與TextView的組合)現(xiàn)在用一個(gè)自定義的TextView就完成能達(dá)到一樣的效果忌堂,并且也可以設(shè)置背景選擇器证舟、圖片的尺寸大小颂暇,不需要嵌套多層布局在設(shè)置相關(guān)的屬性

第一塊Xml中的代碼

    <com.~~~~~~.TextDrawable
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/textdrawable"
        android:clickable="true"
        android:drawablePadding="10dp"
        android:gravity="center_vertical"
        android:padding="@dimen/space_20"
        android:text="設(shè)置"
        android:textColor="@color/black_252c3d"
        android:textSize="@dimen/textsize_20sp_in_normal"
        app:leftDrawable="@drawable/tab_more_unselect"
        app:leftDrawableHeight="@dimen/space_60"
        app:leftDrawableWidth="@dimen/space_60"
        app:rightDrawable="@drawable/iconfont_youjiantou"
        app:rightDrawableHeight="20dp"
        app:rightDrawableWidth="10dp"
        />
    <Space
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/gray_888888"
        />

下面的分割線我建議用Space這個(gè)控件缺谴,它是一個(gè)非常輕量級(jí)的控件

第二塊Xml中的代碼

<com.~~~~~~.TextDrawable
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/textdrawable"
        android:clickable="true"
        android:drawablePadding="10dp"
        android:gravity="center_vertical"
        android:padding="@dimen/space_20"
        android:text="消息"
        android:textColor="@color/black_252c3d"
        android:textSize="@dimen/textsize_20sp_in_normal"
        app:leftDrawable="@drawable/tab_speech_unselect"
        app:leftDrawableHeight="@dimen/space_60"
        app:leftDrawableWidth="@dimen/space_60"
        />

第三塊Xml中的代碼

 <com.~~~~~~.TextDrawable
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@drawable/textdrawable"
            android:clickable="true"
            android:drawablePadding="10dp"
            android:gravity="center"
            android:padding="@dimen/space_20"
            android:text="首頁(yè)"
            android:textColor="@color/colorPrimary"
            android:textSize="@dimen/textsize_20sp_in_normal"
            app:topDrawable="@drawable/tab_home_select"
            app:topDrawableHeight="@dimen/space_60"
            app:topDrawableWidth="@dimen/space_60"
            />

第四塊Xml中的代碼(圖片按鈕)

 <com.hightsstudent.highsstudent.ui.widget.TextDrawable
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/textdrawable"
        android:clickable="true"
        android:drawablePadding="10dp"
        android:gravity="center_vertical"
        android:padding="@dimen/space_15"
        android:text="退出"
        android:textColor="@color/black_252c3d"
        android:textSize="@dimen/textsize_20sp_in_normal"
        app:rightDrawable="@drawable/icon_backs"
        app:rightDrawableHeight="@dimen/space_80"
        app:rightDrawableWidth="@dimen/space_80"
        />

下面貼出TextDrawable.java代碼

/**
 * Created by Dengxiao on 2016/11/8.
 */

public class TextDrawable extends TextView {
    private Drawable drawableLeft;
    private Drawable drawableRight;
    private Drawable drawableTop;
    private int leftWidth;
    private int rightWidth;
    private int topWidth;
    private int leftHeight;
    private int rightHeight;
    private int topHeight;
    private Context mContext;

    public TextDrawable(Context context) {
        this.mContext=context;
        this(context, null, 0);
    }

    public TextDrawable(Context context, AttributeSet attrs) {
        this.mContext=context;
        this(context, attrs, 0);
    }

    public TextDrawable(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextDrawable);
        drawableLeft = typedArray.getDrawable(R.styleable.TextDrawable_leftDrawable);
        drawableRight = typedArray.getDrawable(R.styleable.TextDrawable_rightDrawable);
        drawableTop = typedArray.getDrawable(R.styleable.TextDrawable_topDrawable);
        if (drawableLeft != null) {
            leftWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_leftDrawableWidth, dip2px(context, 20));
            leftHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_leftDrawableHeight, dip2px(context, 20));
        }
        if (drawableRight != null) {
            rightWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_rightDrawableWidth, dip2px(context, 20));
            rightHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_rightDrawableHeight, dip2px(context, 20));
        }
        if (drawableTop != null) {
            topWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_topDrawableWidth, dip2px(context, 20));
            topHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_topDrawableHeight, dip2px(context, 20));
        }
    }


public  int dip2px(Context context, float dpValue) 
  {    
final float scale = context.getResources().getDisplayMetrics().density;    
return (int) (dpValue * scale + 0.5f);
  }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (drawableLeft != null) {
            drawableLeft.setBounds(0, 0, leftWidth, leftHeight);
        }
        if (drawableRight != null) {
            drawableRight.setBounds(0, 0, rightWidth, rightHeight);
        }
        if (drawableTop != null) {
            drawableTop.setBounds(0, 0, topWidth, topHeight);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, null);

    }

    /**
     * 設(shè)置左側(cè)圖片并重繪
     */
    public void setDrawableLeft(Drawable drawableLeft) {
        this.drawableLeft = drawableLeft;
        invalidate();
    }

    /**
     * 設(shè)置左側(cè)圖片并重繪
     */
    public void setDrawableLeft(int drawableLeftRes) {
        this.drawableLeft = mContext.getResources().getDrawable(drawableLeftRes);
        invalidate();
    }

    /**
     * 設(shè)置右側(cè)圖片并重繪
     */
    public void setDrawableRight(Drawable drawableRight) {
        this.drawableRight = drawableLeft;
        invalidate();
    }

    /**
     * 設(shè)置右側(cè)圖片并重繪
     */
    public void setDrawableRight(int drawableRightRes) {
        this.drawableRight = mContext.getResources().getDrawable(drawableRightRes);
        invalidate();
    }

    /**
     * 設(shè)置上部圖片并重繪
     */
    public void setDrawable(Drawable drawableTop) {
        this.drawableTop = drawableTop;
        invalidate();
    }

    /**
     * 設(shè)置右側(cè)圖片并重繪
     */
    public void setDrawableTop(int drawableTopRes) {
        this.drawableTop = mContext.getResources().getDrawable(drawableTopRes);
        invalidate();
    }
}

附上attrs:

<declare-styleable name="TextDrawable">
        <attr name="leftDrawable" format="reference"/>
        <attr name="leftDrawableWidth" format="dimension"/>
        <attr name="leftDrawableHeight" format="dimension"/>
        <attr name="rightDrawable" format="reference"/>
        <attr name="rightDrawableWidth"   format="dimension"/>
        <attr name="rightDrawableHeight" format="dimension"/>
        <attr name="topDrawable" format="reference"/>
        <attr name="topDrawableWidth" format="dimension"/>
        <attr name="topDrawableHeight" format="dimension"/>
    </declare-styleable>

以上為全部代碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市耳鸯,隨后出現(xiàn)的幾起案子湿蛔,更是在濱河造成了極大的恐慌,老刑警劉巖县爬,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件阳啥,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡财喳,警方通過(guò)查閱死者的電腦和手機(jī)察迟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人卷拘,你說(shuō)我怎么就攤上這事喊废。” “怎么了栗弟?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵污筷,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我乍赫,道長(zhǎng)瓣蛀,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任雷厂,我火速辦了婚禮惋增,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘改鲫。我一直安慰自己诈皿,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布像棘。 她就那樣靜靜地躺著稽亏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪缕题。 梳的紋絲不亂的頭發(fā)上截歉,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音烟零,去河邊找鬼瘪松。 笑死,一個(gè)胖子當(dāng)著我的面吹牛锨阿,可吹牛的內(nèi)容都是我干的宵睦。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼墅诡,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼状飞!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起书斜,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤诬辈,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后荐吉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體焙糟,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年样屠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了穿撮。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缺脉。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖悦穿,靈堂內(nèi)的尸體忽然破棺而出攻礼,到底是詐尸還是另有隱情,我是刑警寧澤栗柒,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布礁扮,位于F島的核電站,受9級(jí)特大地震影響瞬沦,放射性物質(zhì)發(fā)生泄漏太伊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一逛钻、第九天 我趴在偏房一處隱蔽的房頂上張望僚焦。 院中可真熱鬧,春花似錦曙痘、人聲如沸芳悲。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)名扛。三九已至,卻和暖如春惩嘉,著一層夾襖步出監(jiān)牢的瞬間罢洲,已是汗流浹背踢故。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工文黎, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人殿较。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓耸峭,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親淋纲。 傳聞我的和親對(duì)象是個(gè)殘疾皇子劳闹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,127評(píng)論 25 707
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 6,415評(píng)論 0 17
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)洽瞬、插件本涕、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,103評(píng)論 4 62
  • 在做有些事情的時(shí)候,我們會(huì)超級(jí)自信伙窃,超級(jí)有力量菩颖,超級(jí)覺得自己是無(wú)敵的,這一刻好像充滿了能量为障,不僅不覺得累晦闰,還想繼續(xù)...
    山澗百合Y閱讀 129評(píng)論 0 0
  • 晚上都快十點(diǎn)了放祟,就想洗臉、泡澡呻右、敷面膜跪妥,舒舒服服的窩在床上看會(huì)兒書!一點(diǎn)都不想有任何人給我打電話声滥,發(fā)微信眉撵,就想有自...
    粟莎閱讀 278評(píng)論 0 1