您的位置:首页 > 移动开发 > Android开发

Android自定义控件之自定义Text,令控件中的字根据控件高度自动调整大小-FenGKun

2014-11-12 16:02 609 查看

/**

* 文字高度会自适应的TextView

*

* @author FenGKun

*

*/

创建AdaptiveText类 继承

public class AdaptiveText extends TextView {
private static String TAG = "AdaptiveText.java";
private static boolean isDebug = false;

/** 是否允许跑马灯效果? */
public static boolean allowMarquee = false;

/** 字大小与控件高的比例 */
private float m_fTextSizeRatioViewHeight = 2.0f / 3;

/** 获取控件宽高比 */
private float m_fAspectRatio = 0f;

public AdaptiveText(Context context) {
super(context);
init(null);
}

public AdaptiveText(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}

public AdaptiveText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}

private void init(AttributeSet attrs) {
// 读取用户配置的参数
if (attrs != null) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyAdaptive);

// 获取字大小与控件高的比例
m_fTextSizeRatioViewHeight = typedArray.getFloat(R.styleable.MyAdaptive_TextSizeRatioViewHeight, 2.0f / 3);

// 获取控件宽高比
m_fAspectRatio = typedArray.getFloat(R.styleable.MyAdaptive_AspectRatio, 0f);

// 获取是否设置了跑马灯效果
if (typedArray.getBoolean(R.styleable.MyAdaptive_Marquee, false)) {
allowMarquee = true;
setSingleLine();
setEllipsize(TextUtils.TruncateAt.MARQUEE);
setMarqueeRepeatLimit(-1);   // 设置循环次数,-1标识无限次
}

typedArray.recycle();
}
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (changed) {
// 字大小与控件高的比例计算出字体的大小(单位像素)
setTextSize(TypedValue.COMPLEX_UNIT_PX, (getHeight() - getPaddingTop() - getPaddingBottom()) * m_fTextSizeRatioViewHeight);
if (isDebug) Log.e(TAG, "字大小与控件高的比例:" + m_fTextSizeRatioViewHeight);
if (isDebug) Log.e(TAG, "设置高度为:" + getHeight() * m_fTextSizeRatioViewHeight);

if (m_fAspectRatio > 0) {
setHeight((int)(getWidth() / m_fAspectRatio));
}
}

super.onLayout(changed, left, top, right, bottom);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if (m_fAspectRatio > 0) {
setHeight((int)(getWidth() / m_fAspectRatio));
}
}

@Override
public boolean isFocused()
{
if (allowMarquee)
{
return true;
}

return super.isFocused();
}

}

布局文件中调用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<span style="color:#ff6666;">xmlns:zf="http://schemas.android.com/apk/res/com.web8848.imf"</span>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlAll"
>
<span style="white-space:pre">	</span><!-- 日期时间 -->
<com.web8848.imf.view.AdaptiveText android:id="@+id/tvDate"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="330"
android:text="2014-10-23 09:42 星期四"
android:textColor="#fff"
android:gravity="center_vertical|right"
android:singleLine="true"
<span style="color:#ff6666;">zf:TextSizeRatioViewHeight="0.4"</span>
/>
</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: