自定义View
来源:互联网 发布:淘宝网高跟鞋 编辑:程序博客网 时间:2024/06/08 16:07
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}第一种方法:继承View实现自定义Viewpublic class MyView extends View { private String text; private int textColor; private float textSize; public MyView(Context context) { super(context); init(null); } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(attrs); } //创建方法 private void init(@Nullable AttributeSet attrs){ TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyView); text = typedArray.getString(R.styleable.MyView_text); textColor=typedArray.getColor(R.styleable.MyView_textcolor,0xffff); textSize=typedArray.getDimension(R.styleable.MyView_textsize,16); } //画 布局设置 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint=new Paint(); paint.setAntiAlias(true); paint.setColor(textColor); paint.setTextSize(textSize); canvas.drawText(text,10,100,paint); }}values下attrs:(第一种方法用)<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="MyView"> <attr name="text" format="string"></attr> <attr name="textsize" format="dimension"></attr> <attr name="textcolor" format="color"></attr></declare-styleable></resources>
第二种方法:继承LinearLayout实现自定义Viewpublic class MyInflatView extends LinearLayout implements View.OnClickListener { private TextView title; private ImageView icon; public MyInflatView(Context context) { super(context); } public MyInflatView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); } private void initView(Context context){ inflate(context,R.layout.custom_view,this); title = (TextView) findViewById(R.id.title); icon = (ImageView) findViewById(R.id.icon); icon.setOnClickListener(this); } @Override public void onClick(View v) { if (v.equals(icon)){ Toast.makeText(getContext(),"点击了",Toast.LENGTH_SHORT).show(); } }}XML: activity<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.fanyishuo.customitem.MainActivity"> <com.example.fanyishuo.customitem.View.MyView android:layout_width="wrap_content" android:layout_height="wrap_content" app:text="小仙女" app:textcolor="#fff0" app:textsize="25sp" android:layout_centerInParent="true" ></com.example.fanyishuo.customitem.View.MyView><com.example.fanyishuo.customitem.View.MyInflatView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"></com.example.fanyishuo.customitem.View.MyInflatView></RelativeLayout>custom_view:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/title" android:text="@string/app_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/icon" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
阅读全文
0 0
- 自定义view
- 自定义View
- 自定义view
- 自定义View
- 自定义View
- 自定义view
- 自定义View
- 自定义view
- 自定义view
- 自定义View
- 自定义View
- 自定义view
- 自定义view
- 自定义view
- 自定义view
- 自定义view
- 自定义View
- 自定义View
- js获取url中的解析字符串
- eclipse JavaScript Validator
- Ilya And The Tree
- 全角、半角字符长度验证
- 浅谈使用Fiddler工具发送post请求(带有json数据)以及get请求(Header方式传参)
- 自定义View
- c# 登录 防止sql注入 mysql数据库
- Ubuntu shutter截图像qq截图一样方便
- ThreadLocal理解
- 最短路径算法—Bellman-Ford(贝尔曼-福特)算法分析与实现(C/C++)
- 从0开始学Python——我的学习之路
- 更新服务器出现问题
- list<T>排列 排序
- python2.7 编码问题