編輯:關於Android編程
如何實現使用TextView的DrawableLeft使圖片和文字居中顯示呢???
代碼如下:
1.首先自定義一個類,繼承TextView
package com.test.signcalendar.weight;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* 自定義TextView,實現drawableLeft可以和文字一起居中
* @author HLQ
* @createtime 2016年3月20日04:14:36
*
*/
public class DrawableCenterTextView extends TextView {
public DrawableCenterTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public DrawableCenterTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawableCenterTextView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
if (drawables != null) {
Drawable drawableLeft = drawables[0];
if (drawableLeft != null) {
float textWidth = getPaint().measureText(getText().toString());
int drawablePadding = getCompoundDrawablePadding();
int drawableWidth = 0;
drawableWidth = drawableLeft.getIntrinsicWidth();
float bodyWidth = textWidth + drawableWidth + drawablePadding;
canvas.translate((getWidth() - bodyWidth) / 2, 0);
}
}
super.onDraw(canvas);
}
}
2.之後在xml布局文件中直接引用即可。。。
<com.test.signcalendar.weight.DrawableCenterTextView
android:id="@+id/textView1111"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/fmhp_mine_health_plan_signcalendar_finish_status_bg"
android:drawablePadding="5dp"//給圖片和文字之間設置填充
android:text="都完成"
android:textColor="#333333"
android:textSize="12sp" />
3。ok實現效果 如下。。。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
Android-屏幕適配需要注意的地方總結
1.盡量使用線性布局(LinearLayout)和相對布局(RelativeLayout),不要使用絕對布局。 2.盡量使用dip和sp,不要使用px。 3.為不同的分辨
Android實現獲取短信驗證碼(Eclipse IDE版)
當前很多APP都有短信驗證的功能,如:帳號與手機號綁定的時候,通過短信驗證的方式確認身份。那麼該如何實現這個功能呢。先簡單說一下流程第一步:獲取短信驗證碼SDK第二步:導
Android內核提取
Android設備啟動的時候有兩種模式:第一種是正常啟動,使用boot分區;第二種是啟動恢復過程,使用recovery分區。這兩種分區的結構是相同的,後面會介紹到。兩者的
android中使用Activity實現監聽手指上下左右滑動
用Activity的onTouchEvent方法實現監聽手指上下左右滑動應用了Activity的ontouchEvent方法監聽手指點擊事件,手指滑動的時候會先按下,滑倒