一个简单的自定义View,可以显示上图下字
来源:互联网 发布:windows iso怎么下载 编辑:程序博客网 时间:2024/05/16 15:22
这个自定义View相对简单,主要是继承了相对布局,最底部的hintText 可以隐藏,当需要的时候才会显示;
上边的图只是显示了 四张自定义控件
<ImageButton app:textSize="10sp" app:src="@drawable/ic_vip" app:text="充油卡" app:srcHeight="30dp" app:textColor="@color/hintTextColor" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/>
布局中引用
现在看看自定义属性
<declare-styleable name="ImageButton"> <!--引用显示的图片--> <attr name="src" format="reference"/> <!--显示的主要文本--> <attr name="text" format="reference|string"/> <!--设置图片的宽度--> <attr name="srcWidth" format="reference"/> <!--设置图片的高度--> <attr name="srcHeight" format="reference|dimension"/> <!--设置字体的高度--> <attr name="textSize" format="dimension|reference"/> <!--设置字体颜色--> <attr name="textColor" format="color|reference"/> <!--设置底部文字的颜色--> <attr name="hintColor" format="color|reference"/> <attr name="hintText" format="string|reference"/> <attr name="hintTextSize" format="dimension|reference"/> </declare-styleable>
下面就来看看里面的代码
public class ImageButton extends LinearLayout { public static final int WRAP_CONTENT = -1; public static final int MATCH_PARENT = -2; private static final int TEXT_SIZE = 12; private final static float HINT_TEXT_SIZE = 10; private static final int PADDING_SIZE = 8; private String text; private int src; private int srcWidth; private int srcHeight; private int textSize; private int textColor; private String hintText; private int hintTextSize; private int hintTextColor; private ImageView mImageView; private TextView mTextView; private TextView mHintTextView; private Paint mPaint; public ImageButton(Context context) { super(context); init(context, null, 0); } public ImageButton(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs, 0); } public ImageButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); } private void init(Context context, AttributeSet attrs, int defStyleAttr) { this.setOrientation(LinearLayout.VERTICAL); initData(); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ImageButton, defStyleAttr, 0); int count = ta.getIndexCount(); for (int i = 0; i < count; i++) { int attr = ta.getIndex(i); switch (attr) { case R.styleable.ImageButton_src: src = ta.getResourceId(attr, -1); break; case R.styleable.ImageButton_text: text = ta.getString(attr); if (text == null) { int stringId = ta.getResourceId(attr, -1); if (stringId != -1) { text = getContext().getString(stringId); } } break; case R.styleable.ImageButton_srcHeight: srcHeight = ta.getDimensionPixelOffset(attr, 0); break; case R.styleable.ImageButton_srcWidth: srcWidth = ta.getDimensionPixelOffset(attr,0); break; case R.styleable.ImageButton_textColor: textColor = ta.getColor(attr, textColor); break; case R.styleable.ImageButton_textSize: textSize = (int) ta.getDimension(attr, TEXT_SIZE); break; case R.styleable.ImageButton_hintColor: hintTextColor = ta.getColor(attr, hintTextColor); break; case R.styleable.ImageButton_hintText: hintText = ta.getString(attr); break; case R.styleable.ImageButton_hintTextSize: hintTextSize = (int) ta.getDimension(attr, -1); break; } } ta.recycle(); mTextView = new TextView(getContext()); LinearLayout.LayoutParams tvLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); tvLp.gravity = Gravity.CENTER_HORIZONTAL; mTextView.setSingleLine(); mTextView.setLayoutParams(tvLp); mTextView.setTextColor(textColor); if (text != null) mTextView.setText(text); mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); mImageView = new ImageView(getContext()); LinearLayout.LayoutParams ivLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); ivLp.weight = srcWidth; ivLp.height = srcHeight; ivLp.gravity = Gravity.CENTER_HORIZONTAL; mImageView.setLayoutParams(ivLp); mImageView.setImageResource(src); LinearLayout.LayoutParams llLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, PADDING_SIZE, getResources().getDisplayMetrics()); setPadding(padding, padding, padding, padding); this.setLayoutParams(llLp); mHintTextView = new TextView(getContext()); LinearLayout.LayoutParams hintTvLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); hintTvLp.gravity = Gravity.CENTER_HORIZONTAL; mHintTextView.setLayoutParams(hintTvLp); if (hintText != null) mHintTextView.setText(hintText); mHintTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, hintTextSize); mHintTextView.setTextColor(hintTextColor); mHintTextView.setSingleLine(); this.addView(mImageView); this.addView(mTextView); this.addView(mHintTextView); } private void initData() { textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, TEXT_SIZE, getResources().getDisplayMetrics()); srcWidth = ViewGroup.LayoutParams.MATCH_PARENT; srcHeight = ViewGroup.LayoutParams.MATCH_PARENT; src = R.mipmap.ic_launcher; text = "测试"; textColor = Color.BLACK; hintTextColor = Color.GRAY; // hintTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,HINT_TEXT_SIZE,getResources().getDisplayMetrics()); mPaint = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //mPaint.setStrokeWidth(10); // mPaint.setColor(Color.RED); //canvas.drawLine(getWidth() - 20, 0, getWidth(), 20, mPaint); } public void setTextSize(int textSize) { mTextView.setTextSize(textSize); } public void setTextSize(int unit, int textSize) { mTextView.setTextSize(unit, textSize); } public void setText(String text) { mTextView.setText(text); } public void setHintText(String hintText) { mHintTextView.setText(hintText); } public void setHintTextSize(int hintTextSize) { mHintTextView.setTextSize(hintTextSize); } public void setHintTextSize(int unit, int hintTextSize) { mHintTextView.setTextSize(unit, hintTextSize); } public void setImageViewBitmap(Bitmap bt) { mImageView.setImageBitmap(bt); } public void setImageView(int btId) { mImageView.setImageResource(btId); } public TextView getTextView() { return mTextView; } public TextView getHintTextView() { return mHintTextView; } public ImageView getImageView() { return mImageView; }}
希望能对您有帮助
0 0
- 一个简单的自定义View,可以显示上图下字
- Android下 一个自定义VIEW实现简单的弹幕效果
- 一个简单的自定义VIEW的DEMO
- 一个简单的Android自定义view详解
- 简单实现一个自定义view的ProgressBar
- 一个简单的Android自定义View
- Android 实现一个简单的自定义View
- Android 自定义View(一) 介绍和一个简单TextView显示
- Android自定义可以在文字上方显示备注的View
- 一个可以实时显示进度的自定义view,类似于listview中音乐播放背景变化的效果
- 自定义BannerView,显示下个一个view部分界面
- 自定义控件,上图下字的Button,图片任意指定大小
- 点击list,可以新增加一个自定义view的效果
- 一个可以在界面顶部展现的自定义 View
- 一个简单的下载网页上图片的程序
- 自定义一个view,并实现最简单的手势识别功能(下)
- UIButton实现上图下字,左图右字等组合形式以及sizeToFit的简单理解
- Android 中GridView上图下字、GridView显示文字
- 先码一个regular expression
- 解决丢失api-ms-win-crt-runtime-|1-1-0.dll的问题
- 高斯滤波
- js中cookie的使用详细分析
- 字符串用法集合
- 一个简单的自定义View,可以显示上图下字
- Extjs 将store等数组格式传入后台计算
- CRC码
- stm8 ADC中断采集
- js 实现用div 上下分屏
- C++ 中引用、指针、const
- UE4 声音
- Error response from daemon: Cannot start container System error: exec: "/bin/bash": stat /bin/bash:
- Frame间最小间隙