赞
踩
- package com.ixgsoft.space;
-
-
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
-
-
- /**
- * @Description:
- */
- public class NewMyElement extends View {
-
-
-
- private String TAG = "NewMyElement";
- private String text;
-
- public NewMyElement(Context context) {
- super(context);
-
- }
- public NewMyElement(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(attrs);
-
-
-
-
- }
- public NewMyElement(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init(attrs);
- }
-
-
- public void init(AttributeSet attrs){
-
- TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.NewMyElement);
-
- String textValue = t.getString(R.styleable.NewMyElement_textValue);
- float textSize = t.getDimension(R.styleable.NewMyElement_textSize, 36);
- int textColor = t.getColor(R.styleable.NewMyElement_textColor, 0xff000000);
- }
-
-
-
-
-
-
-
-
-
- }

- <?xml version="1.0" encoding="utf-8"?>
- <LineanLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:my="http://schemas.android.com/apk/res/com.ixgsoft.space"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
-
- <com.ixgsoft.space.NewMyElement
- my:textValue="草了1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <com.ixgsoft.space.NewMyElement
- my:textValue="草了2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
-
-
- </LineanLayout>

- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="NewMyElement">
- <attr name="textColor" format="color" />
- <attr name="textSize" format="dimension" />
- <attr name="textValue" format="string" />
- </declare-styleable>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <LineanLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:my="http://schemas.android.com/apk/res/com.ixgsoft.space"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
-
- <com.ixgsoft.space.NewMyElement
- my:textValue="草了1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <com.ixgsoft.space.NewMyElement
- my:textValue="草了2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
-
- </LineanLayout>

3.在代码中调用自定义属性
- package com.ixgsoft.space;
-
-
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
-
-
- /**
- * @Description:
- */
- public class NewMyElement extends View {
-
-
-
- private String TAG = "NewMyElement";
- private String text;
-
- public NewMyElement(Context context) {
- super(context);
-
- }
- public NewMyElement(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(attrs);
-
-
-
-
- }
- public NewMyElement(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- init(attrs);
- }
-
-
- public void init(AttributeSet attrs){
-
- TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.NewMyElement);
-
- String textValue = t.getString(R.styleable.NewMyElement_textValue);
- float textSize = t.getDimension(R.styleable.NewMyElement_textSize, 36);
- int textColor = t.getColor(R.styleable.NewMyElement_textColor, 0xff000000);
- }
-
-
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。