寫了一個View ,方便與填寫條例,圖示第一行對齊,第二行一次排列整齊
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatTextView; import android.util.AttributeSet; import com.diction.app.android.R; /** * Created by puyaCheer on 2018/4/7. */ public class TextViewDrawable extends AppCompatTextView { private int drawableLeftWidth, drawableTopWidth, drawableRightWidth, drawableBottomWidth; private int drawableLeftHeight, drawableTopHeight, drawableRightHeight, drawableBottomHeight; private boolean isAliganCenter=true; private boolean isDwMath_content=false; private int mWidth, mHeight; public TextViewDrawable(Context context) { this(context, null); } public TextViewDrawable(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public TextViewDrawable(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs, defStyleAttr); } private void initView(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextViewDrawable); drawableLeftWidth = typedArray.getDimensionPixelSize(R.styleable.TextViewDrawable_drawableLeftWidth, 0); drawableTopWidth = typedArray.getDimensionPixelSize(R.styleable.TextViewDrawable_drawableTopWidth, 0); drawableRightWidth = typedArray.getDimensionPixelSize(R.styleable.TextViewDrawable_drawableRightWidth, 0); drawableBottomWidth = typedArray.getDimensionPixelSize(R.styleable.TextViewDrawable_drawableBottomWidth, 0); drawableLeftHeight = typedArray.getDimensionPixelSize(R.styleable.TextViewDrawable_drawableLeftHeight, 0); drawableTopHeight = typedArray.getDimensionPixelSize(R.styleable.TextViewDrawable_drawableTopHeight, 0); drawableRightHeight = typedArray.getDimensionPixelSize(R.styleable.TextViewDrawable_drawableRightHeight, 0); drawableBottomHeight = typedArray.getDimensionPixelSize(R.styleable.TextViewDrawable_drawableBottomHeight, 0); isAliganCenter = typedArray.getBoolean(R.styleable.TextViewDrawable_isAliganCenter, true); typedArray.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } /* @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = w; mHeight = h; Drawable[] drawables = getCompoundDrawables(); Drawable drawableLeft = drawables[0]; Drawable drawableTop = drawables[1]; Drawable drawableRight = drawables[2]; Drawable drawableBottom = drawables[3]; if (drawableLeft != null) { setDrawable(drawableLeft, 0, drawableLeftWidth, drawableLeftHeight); } if (drawableTop != null) { setDrawable(drawableTop, 1, drawableTopWidth, drawableTopHeight); } if (drawableRight != null) { setDrawable(drawableRight, 2, drawableRightWidth, drawableRightHeight); } if (drawableBottom != null) { setDrawable(drawableBottom, 3, drawableBottomWidth, drawableBottomHeight); } this.setCompoundDrawables(drawableLeft,drawableTop,drawableRight,drawableBottom); } */ @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); mWidth = left; mHeight = top; Drawable[] drawables = getCompoundDrawables(); Drawable drawableLeft = drawables[0]; Drawable drawableTop = drawables[1]; Drawable drawableRight = drawables[2]; Drawable drawableBottom = drawables[3]; if (drawableLeft != null) { setDrawable(drawableLeft, 0, drawableLeftWidth, drawableLeftHeight); } if (drawableTop != null) { setDrawable(drawableTop, 1, drawableTopWidth, drawableTopHeight); } if (drawableRight != null) { setDrawable(drawableRight, 2, drawableRightWidth, drawableRightHeight); } if (drawableBottom != null) { setDrawable(drawableBottom, 3, drawableBottomWidth, drawableBottomHeight); } this.setCompoundDrawables(drawableLeft,drawableTop,drawableRight,drawableBottom); } public void setDrawable(Drawable drawable, int tag, int drawableWidth, int drawableHeight) { //獲取圖片實際長寬 int width = drawableWidth == 0 ? drawable.getIntrinsicWidth() : drawableWidth; int height = drawableHeight == 0 ? drawable.getIntrinsicHeight() : drawableHeight; int left = 0, top = 0, right = 0, bottom = 0; switch (tag) { case 0: case 2: left = 0; top = isAliganCenter ? 0 : -getLineCount() * getLineHeight() / 2 + getLineHeight() / 2; right = width; bottom = top + height; break; case 1: left =isAliganCenter ? 0: -mWidth/2+width/2; top = 0; right = left+width; bottom =top+height; break; } drawable.setBounds(left, top, right, bottom); requestLayout(); } @Override public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) { super.setCompoundDrawables(left, top, right, bottom); } }
===============================對應的屬性============================================
<declare-styleable name="TextViewDrawable"> <attr name="drawableLeftWidth" format="dimension"/> <attr name="drawableTopWidth" format="dimension"/> <attr name="drawableRightWidth" format="dimension"/> <attr name="drawableLeftHeight" format="dimension"/> <attr name="drawableTopHeight" format="dimension"/> <attr name="drawableRightHeight" format="dimension"/> <attr name="drawableBottomHeight" format="dimension"/> <attr name="drawableBottomWidth" format="dimension"/> <attr name="isAliganCenter" format="boolean"/> </declare-styleable>
使用:
<com.diction.app.android.view.TextViewDrawable android:id="@+id/sys_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:drawableLeft="@drawable/message_unread_bg_shape" android:drawablePadding="10dp" android:padding="5dp" android:text="我是標題,你看了我就沒有紅點點我是標題,你看了我就沒有紅點點" android:textColor="@color/color_000000" android:textSize="15sp" app:isAliganCenter="false"/>
如果這介面卡中需要改變的話,類似於訊息已讀未讀
if (helper.getLayoutPosition() == 3){ Drawable drawable= mContext.getResources().getDrawable(R.drawable.message_read_bg_shpe); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); textViewDrawable.setCompoundDrawablesRelative(drawable,null,null,null); textViewDrawable.requestLayout(); }else{ Drawable drawable= mContext.getResources().getDrawable(R.drawable.message_unread_bg_shape); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); textViewDrawable.setCompoundDrawablesRelative(drawable,null,null,null); textViewDrawable.requestLayout(); }
==== 已讀白色背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:useLevel="true"> <size android:width="8dp" android:height="8dp" /> <stroke android:width="2dp" android:color="@color/white" /> <solid android:color="@color/white" /> </shape>
===未讀 紅色背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:useLevel="true"> <size android:width="8dp" android:height="8dp" /> <stroke android:width="2dp" android:color="@color/color_ff3B73" /> <solid android:color="@color/color_ff3B73" /> </shape>
搞定,拷貝直接使用即可
相關文章
- 如何讓文字居右對齊,換行後又居左對齊
- 1218 圖片對齊模式模式
- table上下對齊滾動條設定
- vxe-table 設定單元格對齊方式,左對齊、右對齊
- 第一次寫部落格
- AUTOCAD——圖紙歪瞭如何對齊
- 如何寫好一個自定義ViewView
- linux對齊文字Linux
- 微信小程式——個人中心——view在最前面——一行四個排列微信小程式View
- 【5.31】第一次寫部落格
- 別鬧了,寫個網站其實都不用寫一行程式碼網站行程
- 寫了一個分段函式,引數是單個數字,執行函式 畫圖函式
- 史上第一次!兩大國際知名車企齊現RSAC
- 教你用 css 寫一個擬物化圖示CSS
- canvas textAlign 文字對齊Canvas
- css居中對齊大全CSS
- golang 位元組對齊Golang
- 記憶體對齊記憶體
- 用eclipes寫第一個HelloWorld
- # 編寫第一個Chrome ExtensionChrome
- 編寫第一個Qt程式QT
- Linux C++ 開發2 - 編寫、編譯、執行第一個程式LinuxC++編譯
- ?vue寫的功能最齊全的cnode社群網站Vue網站
- 人類自身都對不齊,怎麼對齊AI?新研究全面審視偏好在AI對齊中的作用AI
- 【IT老齊072】全文檢索執行原理
- 寫一個“特殊”的查詢構造器 – (二、第一條語句)
- 【CSS】段落文字實現兩端對齊,不滿一行則不需要CSS
- 今天寫了一個統計執行sql次數的指令碼SQL指令碼
- CSS文字框與驗證碼垂直對齊CSS
- 特徵工程系列:(三)特徵對齊與表徵特徵工程
- 寫了一個 WebSocket 框架Web框架
- 寫了一個 MySQL 代理MySql
- 鉑金07:整齊劃一-CountDownLatch如何協調多執行緒的開始和結束CountDownLatch執行緒
- 第一次在CSDN上寫部落格
- 第一次嘗試使用java寫sparkJavaSpark
- 綜合RLHF、DPO、KTO優勢,統一對齊框架UNA來了框架
- 齊齊哈爾哪裡有開票-齊齊哈爾開票
- 關聯查詢完,寫個 select 把條件放在外面,方便條件處理