海外主机测评

您现在的位置是:首页 > 服务器 > 正文

服务器

declare-styleabledeclarestyleable

cds8202023-08-23服务器54
本文目录一览:1、android带的属性是怎么定义的2、android绘制进度条3、谁知道融云里面的demo导入到我的工程中报错怎么解决?求专业点解决方法4、andr

本文目录一览:

  • 1、android 带 的 属性是怎么定义的
  • 2、android 绘制进度条
  • 3、谁知道融云里面的demo导入到我的工程中报错怎么解决?求专业点解决方法
  • 4、android中怎么在View构造的attrs中拿到android给的属性
  • 5、自定义view中某条属性指向一个数组,怎么获取
  • 6、android 自定义switch样式

android 带 的 属性是怎么定义的

一、在res/values文件下定义一个attrs.xml文件,代码如下:

?xml versiON="1.0" encoding="utf-8"?

resources

declare-styleable name="ToolBar"

attr name="buttonNum" format="integer"/

attr name="itemBackground" format="reference|color"/

/declare-styleable

/resources

二、在布局xml中如下使用该属性:

?xml version="1.0" encoding="utf-8"?

RelativeLayout xmlns:android=""

xmlns:toolbar=""

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

cn.zzm.toolbar.ToolBar android:id="@+id/gridview_toolbar"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:background="@drawable/control_bar"

android:gravity="center"

toolbar:buttonNum="5"

toolbar:itemBackground="@drawable/control_bar_item_bg"/

/RelativeLayout

三、在自定义组件中,可以如下获得xml中定义的值:

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar);

buttonNum = a.getInt(R.styleable.ToolBar_buttonNum, 5);

itemBg = a.getResourceId(R.styleable.ToolBar_itemBackground, -1);

a.recycle();

就这么简单的三步,即可完成对自定义属性的使用。

*********************************************************************

好了,基本用法已经讲完了,现在来看看一些注意点和知识点吧。

首先来看看attrs.xml文件。

该文件是定义属性名和格式的地方,需要用declare-styleable name="ToolBar"/declare-styleable包围所有属性。其中name为该属性集的名字,主要用途是标识该属性集。那在什么地方会用到呢?主要是在第三步。看到没?在获取某属性标识时,用到"R.styleable.ToolBar_buttonNum",很显然,他在每个属性前面都加了"ToolBar_"。

在来看看各种属性都有些什么类型吧:string , integer , dimension , reference , color , enum.

前面几种的声明方式都是一致的,例如:attr name="buttonNum" format="integer"/。

只有enum是不同的,用法举例:

attr name="testEnum"

enum name="fill_parent" value="-1"/

enum name="wrap_content" value="-2"/

/attr

如果该属性可同时传两种不同的属性,则可以用“|”分割开即可。

让我们再来看看布局xml中需要注意的事项。

首先得声明一下:xmlns:toolbar=

注意,“toolbar”可以换成其他的任何名字,后面的url地址必须最后一部分必须用上自定义组件的包名。自定义属性了,在属性名前加上“toolbar”即可。

最后来看看java代码中的注意事项。

在自定义组件的构造函数中,用

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar);

来获得对属性集的引用,然后就可以用“a”的各种方法来获取相应的属性值了。这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默认值。因此,如果一个属性是带两个及以上不用类型的属性,需要做多次判断,知道读取完毕后才能判断应该赋予何值。当然,在取完值的时候别忘了回收资源哦!

android 绘制进度条

看起来代码挺长,其实都是在获取自定义属性,没什么技术含量。

宽度不变,所以的自定义属性不涉及宽度,高度呢,只考虑不是EXACTLY的情况(用户明确指定了,就不管了),根据padding和进度条宽度算出自己想要的,如果非EXACTLY下,进行exceptHeight封装,传入给控件进行测量高度。

横向的滚动条绘制肯定需要一些属性,比如已/未到达进度的颜色、宽度,文本的颜色、大小等。

本来呢,我是想通过系统ProgressBar的progressDrawable,从里面提取一些属性完成绘制需要的参数的。但是,最终呢,反而让代码变得复杂。所以最终还是改用自定义属性。 说道自定义属性,大家应该已经不陌生了。

1、HorizontalProgressBarWithNumber

1、自定义属性

values/attr_progress_bar.xml:

[html] view plaincopy

?xml version="1.0" encoding="utf-8"?

resources

declare-styleable name="HorizontalProgressBarWithNumber"

attr name="progress_unreached_color" format="color" /

attr name="progress_reached_color" format="color" /

attr name="progress_reached_bar_height" format="dimension" /

attr name="progress_unreached_bar_height" format="dimension" /

attr name="progress_text_size" format="dimension" /

attr name="progress_text_color" format="color" /

attr name="progress_text_offset" format="dimension" /

attr name="progress_text_visibility" format="enum"

enum name="visible" value="0" /

enum name="invisible" value="1" /

/attr

/declare-styleable

declare-styleable name="RoundProgressBarWidthNumber"

attr name="radius" format="dimension" /

/declare-styleable

/resources

2、构造中获取

[java] view plaincopy

public class HorizontalProgressBarWithNumber extends ProgressBar

{

private static final int DEFAULT_TEXT_SIZE = 10;

private static final int DEFAULT_TEXT_COLOR = 0XFFFC00D1;

private static final int DEFAULT_COLOR_UNREACHED_COLOR = 0xFFd3d6da;

private static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2;

private static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2;

private static final int DEFAULT_SIZE_TEXT_OFFSET = 10;

/**

* painter of all drawing things

*/

protected Paint mPaint = new Paint();

/**

* color of progress number

*/

protected int mTextColor = DEFAULT_TEXT_COLOR;

/**

* size of text (sp)

*/

protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);

/**

* offset of draw progress

*/

protected int mTextOffset = dp2px(DEFAULT_SIZE_TEXT_OFFSET);

/**

* height of reached progress bar

*/

protected int mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);

/**

* color of reached bar

*/

protected int mReachedBarColor = DEFAULT_TEXT_COLOR;

/**

* color of unreached bar

*/

protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;

/**

* height of unreached progress bar

*/

protected int mUnReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR);

/**

* view width except padding

*/

protected int mRealWidth;

protected boolean mIfDrawText = true;

protected static final int VISIBLE = 0;

public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs)

{

this(context, attrs, 0);

}

public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs,

int defStyle)

{

super(context, attrs, defStyle);

setHorizontalScrollBarEnabled(true);

obtainStyledAttributes(attrs);

mPaint.setTextSize(mTextSize);

mPaint.setColor(mTextColor);

}

/**

* get the styled attributes

*

* @param attrs

*/

private void obtainStyledAttributes(AttributeSet attrs)

{

// init values from custom attributes

final TypedArray attributes = getContext().obtainStyledAttributes(

attrs, R.styleable.HorizontalProgressBarWithNumber);

mTextColor = attributes

.getColor(

R.styleable.HorizontalProgressBarWithNumber_progress_text_color,

DEFAULT_TEXT_COLOR);

mTextSize = (int) attributes.getDimension(

R.styleable.HorizontalProgressBarWithNumber_progress_text_size,

mTextSize);

mReachedBarColor = attributes

.getColor(

R.styleable.HorizontalProgressBarWithNumber_progress_reached_color,

mTextColor);

mUnReachedBarColor = attributes

.getColor(

R.styleable.HorizontalProgressBarWithNumber_progress_unreached_color,

DEFAULT_COLOR_UNREACHED_COLOR);

mReachedProgressBarHeight = (int) attributes

.getDimension(

R.styleable.HorizontalProgressBarWithNumber_progress_reached_bar_height,

mReachedProgressBarHeight);

mUnReachedProgressBarHeight = (int) attributes

.getDimension(

R.styleable.HorizontalProgressBarWithNumber_progress_unreached_bar_height,

mUnReachedProgressBarHeight);

mTextOffset = (int) attributes

.getDimension(

R.styleable.HorizontalProgressBarWithNumber_progress_text_offset,

mTextOffset);

int textVisible = attributes

.getInt(R.styleable.HorizontalProgressBarWithNumber_progress_text_visibility,

VISIBLE);

if (textVisible != VISIBLE)

{

mIfDrawText = false;

}

attributes.recycle();

}

3、onMeasure

刚才不是出onDraw里面写写就行了么,为什么要改onMeasure呢,主要是因为我们所有的属性比如进度条宽度让用户自定义了,所以我们的测量也得稍微变下。

[java] view plaincopy

@Override

protected synchronized void onMeasure(int widthMeasureSpec,

int heightMeasureSpec)

{

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

if (heightMode != MeasureSpec.EXACTLY)

{

float textHeight = (mPaint.descent() + mPaint.ascent());

int exceptHeight = (int) (getPaddingTop() + getPaddingBottom() + Math

.max(Math.max(mReachedProgressBarHeight,

mUnReachedProgressBarHeight), Math.abs(textHeight)));

heightMeasureSpec = MeasureSpec.makeMeasureSpec(exceptHeight,

MeasureSpec.EXACTLY);

}

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

测量完,就到我们的onDraw了~~~

谁知道融云里面的demo导入到我的工程中报错怎么解决?求专业点解决方法

[-09-08 18:03:18 - android-support-v7-appcompat] Unable to resolve target 'android-19'这个是你的DEMO的适配包含了SDK19你的开发工具没有

[2015-09-08 18:04:08 - android-support-v7-appcompat] ERROR: In

declare-styleable MenuView, unable to find attribute

android:preserveIconSpacing

这个是

eclipse sdk从低版本切换到高版本sdk的时候

v7包会包这个错ERROR: In declare-styleable MenuView, unable to find

attribute android:preserveIconSpacing

问题解决:

点击V7包找到values文件夹 打开attrs.xml ctrl+f 查找 MenuView 将preserveIconSpacing注释掉或者删掉 clean项目

ok 完成。

给后来人看···

android中怎么在View构造的attrs中拿到android给的属性

//Android原生的属性,都是提供方法可以获得的,当然也可以通过

attrs获得,而自定义的属性获得值方式如下,当然原生的也是一样,只需要把attr name该成系统的。

一、 首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:

?xml version="1.0" encoding="utf-8"?

resources

declare-styleable name="my_text_view"

attr name="text_size" format="float"/attr

attr name="text_color" format="color"/attr

attr name="text_back_ground" format="color|reference"/attr

/declare-styleable

/resources

在这里,需要补充attrs属性的相关知识,即Attr属性是如何在XML中定义的,自定义属性的Value值可以有10种类型以及其类型的组合值,其具体使用方法如下:

1. reference:参考某一资源ID。

(1)属性定义:

declare-styleable name = "名称"

attr name = "background" format = "reference" /

/declare-styleable

(2)属性使用:

ImageView

android:layout_width = "42dip"

android:layout_height = "42dip"

android:background = "@drawable/图片ID"

/

2. color:颜色值。

(1)属性定义:

declare-styleable name = "名称"

attr name = "textColor" format = "color" /

/declare-styleable

(2)属性使用:

TextView

android:layout_width = "42dip"

android:layout_height = "42dip"

android:textColor = "#00FF00"

/

3. boolean:布尔值。

(1)属性定义:

declare-styleable name = "名称"

attr name = "focusable" format = "boolean" /

/declare-styleable

(2)属性使用:

Button

android:layout_width = "42dip"

android:layout_height = "42dip"

android:focusable = "true"

/

4. dimension:尺寸值。

(1)属性定义:

declare-styleable name = "名称"

attr name = "layout_width" format = "dimension" /

/declare-styleable

(2)属性使用:

Button

android:layout_width = "42dip"

android:layout_height = "42dip"

/

5. float:浮点值。

(1)属性定义:

declare-styleable name = "AlphaAnimation"

attr name = "fromAlpha" format = "float" /

attr name = "toAlpha" format = "float" /

/declare-styleable

(2)属性使用:

alpha

android:fromAlpha = "1.0"

android:toAlpha = "0.7"

/

6. integer:整型值。

(1)属性定义:

declare-styleable name = "AnimatedRotateDrawable"

attr name = "visible" /

attr name = "frameDuration" format="integer" /

attr name = "framesCount" format="integer" /

attr name = "pivotX" /

attr name = "pivotY" /

attr name = "drawable" /

/declare-styleable

(2)属性使用:

animated-rotate

xmlns:android = ""

android:drawable = "@drawable/图片ID"

android:pivotX = "50%"

android:pivotY = "50%"

android:framesCount = "12"

android:frameDuration = "100"

/

7. string:字符串。

(1)属性定义:

declare-styleable name = "MapView"

attr name = "apiKey" format = "string" /

/declare-styleable

(2)属性使用:

com.google.android.maps.MapView

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"

/

8. fraction:百分数。

(1)属性定义:

declare-styleable name="RotateDrawable"

attr name = "visible" /

attr name = "fromDegrees" format = "float" /

attr name = "toDegrees" format = "float" /

attr name = "pivotX" format = "fraction" /

attr name = "pivotY" format = "fraction" /

attr name = "drawable" /

/declare-styleable

(2)属性使用:

rotate

xmlns:android = ""

android:interpolator = "@anim/动画ID"

android:fromDegrees = "0"

android:toDegrees = "360"

android:pivotX = "200%"

android:pivotY = "300%"

android:duration = "5000"

android:repeatMode = "restart"

android:repeatCount = "infinite"

/

9. enum:枚举值。

(1)属性定义:

declare-styleable name="名称"

attr name="orientation"

enum name="horizontal" value="0" /

enum name="vertical" value="1" /

/attr

/declare-styleable

(2)属性使用:

LinearLayout

xmlns:android = ""

android:orientation = "vertical"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

/LinearLayout

10. flag:位或运算。

(1)属性定义:

declare-styleable name="名称"

attr name="windowSoftInputMode"

flag name = "stateUnspecified" value = "0" /

flag name = "stateUnchanged" value = "1" /

flag name = "stateHidden" value = "2" /

flag name = "stateAlwaysHidden" value = "3" /

flag name = "stateVisible" value = "4" /

flag name = "stateAlwaysVisible" value = "5" /

flag name = "adjustUnspecified" value = "0x00" /

flag name = "adjustResize" value = "0x10" /

flag name = "adjustPan" value = "0x20" /

flag name = "adjustNothing" value = "0x30" /

/attr

/declare-styleable

(2)属性使用:

activity

android:name = ".StyleAndThemeActivity"

android:label = "@string/app_name"

android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"

intent-filter

action android:name = "android.intent.action.MAIN" /

category android:name = "android.intent.category.LAUNCHER" /

/intent-filter

/activity

注意:

属性定义时可以指定多种类型值。

(1)属性定义:

declare-styleable name = "名称"

attr name = "background" format = "reference|color" /

/declare-styleable

(2)属性使用:

ImageView

android:layout_width = "42dip"

android:layout_height = "42dip"

android:background = "@drawable/图片ID|#00FF00"

/

二、接下来实现自定义View的类,其中下面的构造方法是重点,在代码中获取自定义属性,其代码如下:

package com.example.CustomAttr;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.util.TypedValue;

import android.widget.TextView;

/**

* Created with IntelliJ IDEA.

* User: zhiwen.nan

* Date: 13-9-28

* Time: 下午10:00

* To change this template use File | Settings | File Templates.

*/

public class CustomTextView extends TextView {

private TypedArray mTypedArray;

private Paint mPaint;

public CustomTextView(Context context) {

super(context);

}

public CustomTextView(Context context, AttributeSet attrs) {

super(context, attrs);

inial(context,attrs);

}

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

inial(context,attrs);

}

private void inial(Context context,AttributeSet attrs) {

TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.my_text_view);

float textsize = typedArray.getFloat(R.styleable.my_text_view_text_size,14) ;

int textColor = typedArray.getColor(R.styleable.my_text_view_text_color,0xFFFFFF) ;

int bgColor = typedArray.getColor(R.styleable.my_text_view_text_back_ground,0xFFFFFF) ;

super.setTextColor(textColor);

super.setTextSize(textsize);

super.setBackgroundColor(bgColor);

typedArray.recycle();

}

}

三、接下来在XML布局中引用自定义View控件,其XML代码如下:

?xml version="1.0" encoding="utf-8"?

LinearLayout xmlns:android=""

xmlns:app = ""

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

com.example.CustomAttr.CustomTextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Hello World, MainActivity"

app:text_size ="20"

app:text_color ="#00FF00"

app:text_back_ground="#ffffff"

/

/LinearLayout

注意上面XML中代码: xmlns:app = "",是自定义的app命名空间,res后面是应用程序包名,然后可以直接使用app:text_size,等属性,其值类型要和attrs.xml定义的属性Value值相对应。

四、总结:

注意该例子中是使用app:text_size = "20 和app:text_color="#00FF00定义TextView的颜色和textView的字体大小,而不是使用系统的属性android:textsize等。该例子中只是起到抛砖引玉的作用,你可以自定义其他属性,来实现你想要的自定义View效果。

自定义view中某条属性指向一个数组,怎么获取

首先定义一下自定义属性,一种好的习惯是自定义的属性集合的名字要和使用这些属性的自定义View的类名一致,当然, 这个也不是必须的, 比如如下的属性集合, 也可以用在OtherCustomeView里。

declare-styleable name="DrawableTextView"

   attr name="drawableHeight" format="dimension"/

   attr name="drawableWidth" format="dimension"/

/declare-styleable

布局文件中定义DrawableTextView的时候, 就可以使用定义好的自定义属性了,如app:开头的属性。

com.shwy.bestjoy.view.DrawableTextView

    xmlns:app="

    android:id="@+id/model_my_customer"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:textColor="@color/defaultBlueButtonColor"

    android:textSize="22sp"

    android:text="@string/model_my_customer"

    android:gravity="center_vertical"

    android:drawableLeft="@drawable/model_my_customer_icon"

    android:drawablePadding="10dip"

    app:drawableHeight="45dip"

    app:drawableWidth="45dip" /

在DrawableTextView.java类里读出属性值

public DrawableTextView(Context context, AttributeSet attrs) {

    super(context, attrs);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);

    //图片的高度

    mDrawableHeight = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableHeight, 0);

    mDrawableWidth = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableWidth, 0);

    typedArray.recycle();

}

android 自定义switch样式

修改后的MySwitch控件接口基本与原Switch控件一致,并且除了可支持所有SDK外,增加了2项小功能:

1. 支持用Track背景图片的方式代替Texton Textoff等文字方式表现开关状态

2.支持调整控制Switch的高度

下面贴出Switch修改的关键代码:

/**

* p

* modified from android SDK 14(4.0) android.widget.Switch.

* br/

* strongnew feature: /strong

* ol

* lisupport SDK 1 or higher. /li

* liyou can use track drawable instead of text to display the changes of off-on state!/li

* liyou can control the Switch minimum height. /li

* /ol

* /p

*

* @see {@link Switch}

* @author Wison

*/

public class MySwitch extends CompoundButton {

// Support track drawable instead of text

private Drawable mTrackOnDrawable;

private Drawable mTrackOffDrawable;

// Support minimum height

private int mSwitchMinHeight;

/**

* Construct a new Switch with a default style determined by the given theme attribute,

* overriding specific style attributes as requested.

*

* @param context The Context that will determine this widget's theming.

* @param attrs Specification of attributes that should deviate from the default styling.

* @param defStyle An attribute ID within the active theme containing a reference to the

* default style for this widget. e.g. android.R.attr.switchStyle.

*/

public MySwitch(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

Resources res = getResources();

mTextPaint.density = res.getDisplayMetrics().density;

//float scaledDensity = res.getDisplayMetrics().scaledDensity;

//mTextPaint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Switch, defStyle, 0);

// off-on 模式: 图片模式或文字模式,图片模式是用Track背景图片表示off-on的状态,文字模式是用文字来表示off-on状态。

mTrackOnDrawable = a.getDrawable(R.styleable.Switch_trackOn);

mTrackOffDrawable = a.getDrawable(R.styleable.Switch_trackOff);

if (checkTrackOffOnDrawable()) {

// 如果设定图片模式,则默认显示off状态

mTrackDrawable = mTrackOffDrawable;

} else {

mTrackDrawable = a.getDrawable(R.styleable.Switch_track);

}

mThumbDrawable = a.getDrawable(R.styleable.Switch_thumb);

mTextOn = a.getText(R.styleable.Switch_textOn);

mTextOff = a.getText(R.styleable.Switch_textOff);

mThumbTextPadding = a.getDimensionPixelSize(R.styleable.Switch_thumbTextPadding, 0);

mSwitchMinWidth = a.getDimensionPixelSize(R.styleable.Switch_switchMinWidth, 0);

mSwitchMinHeight = a.getDimensionPixelSize(R.styleable.Switch_switchMinHeight, 0);

mSwitchPadding = a.getDimensionPixelSize(R.styleable.Switch_switchPadding, 0);

int appearance = a.getResourceId(R.styleable.Switch_switchTextAppearance, 0);

if (appearance != 0) {

setSwitchTextAppearance(context, appearance);

}

a.recycle();

ViewConfiguration config = ViewConfiguration.get(context);

mTouchSlop = config.getScaledTouchSlop();

mMinFlingVelocity = config.getScaledMinimumFlingVelocity();

// Refresh display with current params

refreshDrawableState();

setChecked(isChecked());

}

private boolean checkTrackOffOnDrawable() {

return mTrackOnDrawable != null mTrackOffDrawable != null;

}

@SuppressLint("NewApi")

@Override

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

if (mOnLayout == null) {

mOnLayout = makeLayout(mTextOn);

}

if (mOffLayout == null) {

mOffLayout = makeLayout(mTextOff);

}

mTrackDrawable.getPadding(mTempRect);

final int maxTextWidth = Math.max(mOnLayout.getWidth(), mOffLayout.getWidth());

final int switchWidth = Math.max(mSwitchMinWidth,

maxTextWidth * 2 + mThumbTextPadding * 4 + mTempRect.left + mTempRect.right);

// final int switchHeight = mTrackDrawable.getIntrinsicHeight();

int switchHeight;

if (mSwitchMinHeight = 0) {

switchHeight = mTrackDrawable.getIntrinsicHeight();

} else {

switchHeight = Math.max(mSwitchMinHeight, mTempRect.top + mTempRect.bottom);

}

mThumbWidth = maxTextWidth + mThumbTextPadding * 2;

mSwitchWidth = switchWidth;

mSwitchHeight = switchHeight;

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

final int measuredHeight = getMeasuredHeight();

if (measuredHeight switchHeight) {

if (Build.VERSION.SDK_INT = 11) {

setMeasuredDimension(getMeasuredWidthAndState(), switchHeight);

} else {

setMeasuredDimension(getMeasuredWidth(), switchHeight);

}

}

}

@Override

public void setChecked(boolean checked) {

if (checkTrackOffOnDrawable()) {

mTrackDrawable = checked ? mTrackOnDrawable : mTrackOffDrawable;

refreshDrawableState();

}

super.setChecked(checked);

mThumbPosition = checked ? getThumbScrollRange() : 0;

invalidate();

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

// Draw the switch

int switchLeft = mSwitchLeft;

int switchTop = mSwitchTop;

int switchRight = mSwitchRight;

int switchBottom = mSwitchBottom;

if (checkTrackOffOnDrawable()) {

mTrackDrawable = getTargetCheckedState() ? mTrackOnDrawable : mTrackOffDrawable;

refreshDrawableState();

}

mTrackDrawable.setBounds(switchLeft, switchTop, switchRight, switchBottom);

mTrackDrawable.draw(canvas);

canvas.save()

mTrackDrawable.getPadding(mTempRect);

int switchInnerLeft = switchLeft + mTempRect.left;

int switchInnerTop = switchTop + mTempRect.top;

int switchInnerRight = switchRight - mTempRect.right;

int switchInnerBottom = switchBottom - mTempRect.bottom;

canvas.clipRect(switchInnerLeft, switchTop, switchInnerRight, switchBottom);

mThumbDrawable.getPadding(mTempRect);

final int thumbPos = (int) (mThumbPosition + 0.5f);

int thumbLeft = switchInnerLeft - mTempRect.left + thumbPos;

int thumbRight = switchInnerLeft + thumbPos + mThumbWidth + mTempRect.right;

mThumbDrawable.setBounds(thumbLeft, switchTop, thumbRight, switchBottom);

mThumbDrawable.draw(canvas);

// mTextColors should not be null, but just in case

if (mTextColors != null) {

mTextPaint.setColor(mTextColors.getColorForState(getDrawableState(),

mTextColors.getDefaultColor()));

}

mTextPaint.drawableState = getDrawableState();

Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;

if (switchText != null) {

canvas.translate((thumbLeft + thumbRight) / 2 - switchText.getWidth() / 2,

(switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2);

switchText.draw(canvas);

}

canvas.restore();

}

}

下面是关键属性声明:

declare-styleable name="Switch"

!-- Drawable to use when the switch is in the checked/"on" state. --

attr name="trackOn" format="reference" /

!-- Drawable to use when the switch is in the unchecked/"off" state. --

attr name="trackOff" format="reference" /

!-- Minimum height for the switch component --

attr name="switchMinHeight" format="dimension" /

!-- Drawable to use as the "thumb" that switches back and forth. --

attr name="thumb" format="reference" /

!-- Drawable to use as the "track" that the switch thumb slides within. --

attr name="track" format="reference" /

!-- Text to use when the switch is in the checked/"on" state. --

attr name="textOn" format="string" /

!-- Text to use when the switch is in the unchecked/"off" state. --

attr name="textOff" format="string" /

!-- Amount of padding on either side of text within the switch thumb. --

attr name="thumbTextPadding" format="dimension" /

!-- TextAppearance style for text displayed on the switch thumb. --

attr name="switchTextAppearance" format="reference" /

!-- Minimum width for the switch component --

attr name="switchMinWidth" format="dimension" /

!-- Minimum space between the switch and caption text --

attr name="switchPadding" format="dimension" /

/declare-styleable

关于declare-styleable和declarestyleable的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~