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>
相关文章推荐
- Android自定义控件之自定义EditText,令控件中的字根据控件高度自动调整大小-FenGKun
- android 字符串根据textview宽度 自动调整自身大小
- android开发 textview根据字数长度自动调整字体大小
- Android 自定义TextView实现文本内容自动调整字体大小以适应TextView的大小
- Android 自定义TextView实现文本内容自动调整字体大小以适应TextView的大小
- Android 自定义TextView实现文本内容自动调整字体大小
- Android 自定义TextView实现文本内容自动调整字体大小以适应TextView的大小
- Android自定义TextView根据控件高度和宽度,解决字体适配问题
- android根据分辨率自动调整字体大小的实例代码
- android根据分辨率自动调整字体大小的实例代码
- android中自动调整控件内字体大小
- android 字体大小 根据分辨率 自动调整
- Android自定义控件之自定义Text,画出米字格-FenGKun
- 笔记:把界面上静态文本控件的尺寸根据文本的内容自动调整大小
- android字体大小根据分辨率自动调整
- 根据页面内容高度自动调整弹窗大小 推荐
- Android ListView根据项数的大小自动改变高度
- 根据控件大小自动调整字体大小方法(C#)
- Android-自定义控件之重写控件(自定义TextView)
- Android 根据屏幕分辨率自动调整字体大小