自定义View组合模式
16lz
2021-01-26
attrs.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="ImageText"> <attr name="src" format="reference|color" /> <attr name="imageWidth" format="dimension" /> <attr name="imageHeight" format="dimension" /> <attr name="textSize" format="dimension" /> <attr name="text" format="string" /> declare-styleable>resources>
组合控件的布局view_image_text.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" > <ImageView android:id="@+id/iv_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@drawable/selector_student_text" />LinearLayout>
activity_main.xml:
"http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:it="http://schemas.android.com/apk/res-auto"> id="@+id/it_test" it:src="@mipmap/ic_launcher" it:imageWidth="20dp" it:imageHeight="20dp" it:text="hello" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
drawable
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@android:color/holo_blue_dark" android:state_selected="true"/> <item android:color="@android:color/holo_blue_dark" android:state_checked="true"/> <item android:color="@android:color/holo_blue_dark" android:state_pressed="true"/> <item android:color="@android:color/darker_gray"/>selector>
MainActivity.java
package com.itant.customedview;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener { private ImageText it_test; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); it_test = (ImageText) findViewById(R.id.it_test); it_test.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(this, "click", Toast.LENGTH_SHORT).show(); }}
自定义控件:
package com.itant.customedview;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class ImageText extends LinearLayout { private TextView textView; public ImageText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub LayoutInflater.from(context).inflate(R.layout.view_image_text, this); ImageView imageView = (ImageView) findViewById(R.id.iv_image); textView = (TextView) findViewById(R.id.tv_text); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageText); int imageResource = typedArray.getResourceId(R.styleable.ImageText_src, 0); int imageWidth = UIUtils.dip2px(context, typedArray.getDimension(R.styleable.ImageText_imageWidth, 40)); int imageHeight = UIUtils.dip2px(context, typedArray.getDimension(R.styleable.ImageText_imageHeight, 40)); float textSize = typedArray.getDimension(R.styleable.ImageText_textSize, 12); String text = typedArray.getString(R.styleable.ImageText_text); typedArray.recycle(); imageView.setBackgroundResource(imageResource); LayoutParams params = new LayoutParams(imageWidth, imageHeight); imageView.setLayoutParams(params); textView.setTextSize(textSize); textView.setText(text); } /* @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: textView.setTextColor(getResources().getColor(R.color.red)); break; case MotionEvent.ACTION_UP: textView.setTextColor(getResources().getColor(R.color.gray)); break; default: break; } //performClick(); return super.onTouchEvent(event); }*/}
github传送
更多相关文章
- [控件]SeekBar拖动条
- Android(安卓)UI控件-Spinner(下拉列表)
- android text 中英文混排 换行的问题
- 去除Dialog边框
- Android(安卓)控件布局常用属性
- 安卓倒计时
- [Android]Fragment点击穿透问题
- 弹出式菜单PopMenu
- Android(安卓)在自定义view中动态设置布局规则