reprint: http://www.jianshu.com/p/d3027acf475aToday to share a layout of a small trick, I hope you can also write a better performance of the layout, my personal purpose is to use the minimum view to write the same effect layout with textview simultaneously display pictures and text:
Check it out first.
Image 3.png above these four blocks of the area believe that you often encounter in the project! (General notation ImageView and TextView combination) now with a custom textview can achieve the same effect, and can also set the background selector, the size of the picture, do not need to nest multi-layer layout in the first block of the relevant properties of XML code
<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="Settings" 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"/>
Below the split line I suggest using space for this control, which is a very lightweight control of the code in the second block of 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="message "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 "/>
The code in the third block of 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=" home "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 "/>
The code in the XML (Picture button)
<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= "exit" 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"/>
Post the Textdrawable.java code below
/** * Created by Dengxiao on 2016/11/8. */PublicClass Textdrawable extends TextView {Private drawable Drawableleft;Private drawable drawableright;Private drawable Drawabletop;Privateint leftwidth;Privateint rightwidth;Privateint topwidth;Privateint leftheight;Privateint rightheight;Privateint topheight;Private Context Mcontext;PublicTextdrawable(Context context) {This.mcontext=context;This (context, NULL,0); }PublicTextdrawable(context context, AttributeSet attrs) {This.mcontext=context;This (context, attrs,0); }PublicTextdrawable(context context, AttributeSet attrs,int defstyleattr) {Super (context, attrs, defstyleattr);This.mcontext=context; Init (context, attrs); }PrivatevoidInit(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)); } }PublicIntdip2px(Context context,Float Dpvalue) {finalFloat scale = context.getresources (). Getdisplaymetrics (). density;Return (int) (Dpvalue * scale +0.5f); } @OverrideProtectedvoidOnmeasure(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); } } @OverrideProtectedvoidOnDraw(Canvas canvas) {Super.ondraw (canvas);This.setcompounddrawables (Drawableleft, drawabletop, drawableright, NULL); }/** * Set picture on left and redraw */PublicvoidSetdrawableleft(drawable Drawableleft) {This.drawableleft = Drawableleft; Invalidate (); }/** * Set picture on left and redraw */PublicvoidSetdrawableleft(int drawableleftres) {This.drawableleft = Mcontext.getresources (). getdrawable (Drawableleftres); Invalidate (); }/** * Set right picture and redraw */PublicvoidSetdrawableright(drawable drawableright) {This.drawableright = Drawableleft; Invalidate (); }/** * Set right picture and redraw */PublicvoidSetdrawableright (int drawablerightres) { This.drawableright = Mcontext.getresources (). getdrawable (Drawablerightres); Invalidate (); } /** * set upper picture and redraw */public void setdrawable (Drawable DrawableTop) { Span class= "Hljs-keyword" >this.drawabletop = drawabletop; Invalidate (); } /** * set right picture and redraw */public void setdrawabletop ( int drawabletopres) {this.drawabletop = Mcontext.getresources (). getdrawable (Drawabletopres); invalidate ();}}
Attached attrs:
<Declare-styleableName="Textdrawable" ><attrName="Leftdrawable"format="Reference"/><attrName="Leftdrawablewidth"format="Dimension"/><attrName="Leftdrawableheight"format="Dimension"/><attrName="Rightdrawable"format="Reference"/><attrName="Rightdrawablewidth"format="Dimension"/><attrName= "rightdrawableheight" format= " Dimension "/> <attr name= "topdrawable" format= "reference"/> Span class= "Hljs-tag" ><attr name= "topdrawablewidth" format= " Dimension "/> <attr name= "topdrawableheight" format= "Dimension" /> </DECLARE-STYLEABLE>
The above is all code
3.Android Optimized layout (solves TextView layout)