android - 自定义(组合)控件 + 自定义控件外观

谁践踏了优雅 2021-12-20 06:27 656阅读 0赞

Android自定义View实现很简单

继承View,重写构造函数、onDraw,(onMeasure)等函数。

如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。

在使用到自定义View的xml布局文件中需要加入xmlns:前缀=”http://schemas.android.com/apk/res/你的应用所在的包路径“.

在使用自定义属性的时候,使用前缀:属性名,如my:textColor=”#FFFFFFF”。

实例:

  1. package demo.view.my;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Paint.Style;
  8. import android.util.AttributeSet;
  9. import android.view.View;
  10. /**
  11. * 这个是自定义的TextView.
  12. * 至少需要重载构造方法和onDraw方法
  13. * 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了
  14. * 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称
  15. * 并根据需要设定默认值,放在在xml文件中没有定义。
  16. * 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
  17. * 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
  18. * 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们应用所在的包
  19. * @author Administrator
  20. *
  21. */
  22. public class MyView extends View {
  23. Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息
  24. public MyView(Context context) {
  25. super(context);
  26. }
  27. public MyView(Context context, AttributeSet attrs){
  28. super(context, attrs);
  29. mPaint = new Paint();
  30. //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组
  31. //在使用完成后,一定要调用recycle方法
  32. //属性的名称是styleable中的名称+“_”+属性名称
  33. TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
  34. int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值,放置未指定
  35. float textSize = array.getDimension(R.styleable.MyView_textSize, 36);
  36. mPaint.setColor(textColor);
  37. mPaint.setTextSize(textSize);
  38. array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响
  39. }
  40. public void onDraw(Canvas canvas){
  41. super.onDraw(canvas);
  42. //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形
  43. //mPaint = new Paint();
  44. //mPaint.setColor(Color.RED);
  45. mPaint.setStyle(Style.FILL); //设置填充
  46. canvas.drawRect(10, 10, 100, 100, mPaint); //绘制矩形
  47. mPaint.setColor(Color.BLUE);
  48. canvas.drawText("我是被画出来的", 10, 120, mPaint);
  49. }
  50. }

相应的属性文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="MyView">
  4. <attr name="textColor" format="color"/>
  5. <attr name="textSize" format="dimension"/>
  6. </declare-styleable>
  7. </resources>

在布局文件中的使用:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <demo.view.my.MyView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. my:textColor="#FFFFFFFF"
  12. my:textSize="22dp"
  13. />
  14. </LinearLayout>

注意事项:

做Android布局是件很享受的事,这得益于他良好的xml方式。使用xml可以快速 有效的为软件定义界面。可是有时候我们总感觉官方定义的一些基本组件不够用,自定义组件就不可避免了。那么如何才能做到像官方提供的那些组件一样用xml 来定义他的属性呢?现在我们就来讨论一下他的用法。

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

<?xml version=”1.0” encoding=”utf-8”?>






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

<?xml version=”1.0” encoding=”utf-8”?>



三、在自定义组件中,可以如下获得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文件。

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

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

前面几种的声明方式都是一致的,例如:
只有enum是不同的,用法举例:




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

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

首先得声明一下:xmlns:toolbar=http://schemas.android.com/apk/res/cn.zzm.toolbar
注意,“toolbar”可以换成其他的任何名字,后面的url地址必须最后一部分必须用上自定义组件的包名。自定义属性了,在属性名前加上“toolbar”即可。

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

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

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

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

属性详解:

  1. 1. reference:参考某一资源ID
  2. 1)属性定义:
  3. <declare-styleable name = "名称">
  4. <attr name = "background" format = "reference" />
  5. </declare-styleable>
  6. 2)属性使用:
  7. <ImageView
  8. android:layout_width = "42dip"
  9. android:layout_height = "42dip"
  10. android:background = "@drawable/图片ID"
  11. />
  12. 2. color:颜色值。
  13. 1)属性定义:
  14. <declare-styleable name = "名称">
  15. <attr name = "textColor" format = "color" />
  16. </declare-styleable>
  17. 2)属性使用:
  18. <TextView
  19. android:layout_width = "42dip"
  20. android:layout_height = "42dip"
  21. android:textColor = "#00FF00"
  22. />
  23. 3. boolean:布尔值。
  24. 1)属性定义:
  25. <declare-styleable name = "名称">
  26. <attr name = "focusable" format = "boolean" />
  27. </declare-styleable>
  28. 2)属性使用:
  29. <Button
  30. android:layout_width = "42dip"
  31. android:layout_height = "42dip"
  32. android:focusable = "true"
  33. />
  34. 4. dimension:尺寸值。
  35. 1)属性定义:
  36. <declare-styleable name = "名称">
  37. <attr name = "layout_width" format = "dimension" />
  38. </declare-styleable>
  39. 2)属性使用:
  40. <Button
  41. android:layout_width = "42dip"
  42. android:layout_height = "42dip"
  43. />
  44. 5. float:浮点值。
  45. 1)属性定义:
  46. <declare-styleable name = "AlphaAnimation">
  47. <attr name = "fromAlpha" format = "float" />
  48. <attr name = "toAlpha" format = "float" />
  49. </declare-styleable>
  50. 2)属性使用:
  51. <alpha
  52. android:fromAlpha = "1.0"
  53. android:toAlpha = "0.7"
  54. />
  55. 6. integer:整型值。
  56. 1)属性定义:
  57. <declare-styleable name = "AnimatedRotateDrawable">
  58. <attr name = "visible" />
  59. <attr name = "frameDuration" format="integer" />
  60. <attr name = "framesCount" format="integer" />
  61. <attr name = "pivotX" />
  62. <attr name = "pivotY" />
  63. <attr name = "drawable" />
  64. </declare-styleable>
  65. 2)属性使用:
  66. <animated-rotate
  67. xmlns:android = "http://schemas.android.com/apk/res/android"
  68. android:drawable = "@drawable/图片ID"
  69. android:pivotX = "50%"
  70. android:pivotY = "50%"
  71. android:framesCount = "12"
  72. android:frameDuration = "100"
  73. />
  74. 7. string:字符串。
  75. 1)属性定义:
  76. <declare-styleable name = "MapView">
  77. <attr name = "apiKey" format = "string" />
  78. </declare-styleable>
  79. 2)属性使用:
  80. <com.google.android.maps.MapView
  81. android:layout_width = "fill_parent"
  82. android:layout_height = "fill_parent"
  83. android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
  84. />
  85. 8. fraction:百分数。
  86. 1)属性定义:
  87. <declare-styleable name="RotateDrawable">
  88. <attr name = "visible" />
  89. <attr name = "fromDegrees" format = "float" />
  90. <attr name = "toDegrees" format = "float" />
  91. <attr name = "pivotX" format = "fraction" />
  92. <attr name = "pivotY" format = "fraction" />
  93. <attr name = "drawable" />
  94. </declare-styleable>
  95. 2)属性使用:
  96. <rotate
  97. xmlns:android = "http://schemas.android.com/apk/res/android"
  98.    android:interpolator = "@anim/动画ID"
  99. android:fromDegrees = "0"
  100.    android:toDegrees = "360"
  101. android:pivotX = "200%"
  102. android:pivotY = "300%"
  103.    android:duration = "5000"
  104. android:repeatMode = "restart"
  105. android:repeatCount = "infinite"
  106. />
  107. 9. enum:枚举值。
  108. 1)属性定义:
  109. <declare-styleable name="名称">
  110. <attr name="orientation">
  111. <enum name="horizontal" value="0" />
  112. <enum name="vertical" value="1" />
  113. </attr>
  114. </declare-styleable>
  115. 2)属性使用:
  116. <LinearLayout
  117. xmlns:android = "http://schemas.android.com/apk/res/android"
  118. android:orientation = "vertical"
  119. android:layout_width = "fill_parent"
  120. android:layout_height = "fill_parent"
  121. >
  122. </LinearLayout>
  123. 10. flag:位或运算。
  124. 1)属性定义:
  125. <declare-styleable name="名称">
  126. <attr name="windowSoftInputMode">
  127. <flag name = "stateUnspecified" value = "0" />
  128. <flag name = "stateUnchanged" value = "1" />
  129. <flag name = "stateHidden" value = "2" />
  130. <flag name = "stateAlwaysHidden" value = "3" />
  131. <flag name = "stateVisible" value = "4" />
  132. <flag name = "stateAlwaysVisible" value = "5" />
  133. <flag name = "adjustUnspecified" value = "0x00" />
  134. <flag name = "adjustResize" value = "0x10" />
  135. <flag name = "adjustPan" value = "0x20" />
  136. <flag name = "adjustNothing" value = "0x30" />
  137. </attr>
  138. </declare-styleable>
  139. 2)属性使用:
  140. <activity
  141. android:name = ".StyleAndThemeActivity"
  142. android:label = "@string/app_name"
  143. android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
  144. <intent-filter>
  145. <action android:name = "android.intent.action.MAIN" />
  146. <category android:name = "android.intent.category.LAUNCHER" />
  147. </intent-filter>
  148. </activity>
  149. 注意:
  150. 属性定义时可以指定多种类型值。
  151. 1)属性定义:
  152. <declare-styleable name = "名称">
  153. <attr name = "background" format = "reference|color" />
  154. </declare-styleable>
  155. 2)属性使用:
  156. <ImageView
  157. android:layout_width = "42dip"
  158. android:layout_height = "42dip"
  159. android:background = "@drawable/图片ID|#00FF00"
  160. />
  161. 自定义组合控件:
  162. 第一个实现一个带图片和文字的按钮,如图所示:

2011080722250185.png

整个过程可以分四步走。第一步,定义一个layout,实现按钮内部的布局。代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ImageView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/iv"
  11. android:src="@drawable/confirm"
  12. android:paddingTop="5dip"
  13. android:paddingBottom="5dip"
  14. android:paddingLeft="40dip"
  15. android:layout_gravity="center_vertical"
  16. />
  17. <TextView
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="确定"
  21. android:textColor="#000000"
  22. android:id="@+id/tv"
  23. android:layout_marginLeft="8dip"
  24. android:layout_gravity="center_vertical"
  25. />
  26. </LinearLayout>

这个xml实现一个左图右字的布局,接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。代码如下:

  1. package com.notice.ib;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.LayoutInflater;
  5. import android.widget.ImageView;
  6. import android.widget.LinearLayout;
  7. import android.widget.TextView;
  8. public class ImageBt extends LinearLayout {
  9. private ImageView iv;
  10. private TextView tv;
  11. public ImageBt(Context context) {
  12. this(context, null);
  13. }
  14. public ImageBt(Context context, AttributeSet attrs) {
  15. super(context, attrs);
  16. // 导入布局
  17. LayoutInflater.from(context).inflate(R.layout.custombt, this, true);
  18. iv = (ImageView) findViewById(R.id.iv);
  19. tv = (TextView) findViewById(R.id.tv);
  20. }
  21. /**
  22. * 设置图片资源
  23. */
  24. public void setImageResource(int resId) {
  25. iv.setImageResource(resId);
  26. }
  27. /**
  28. * 设置显示的文字
  29. */
  30. public void setTextViewText(String text) {
  31. tv.setText(text);
  32. }
  33. }

第三步,在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。方法如下:

  1. <RelativeLayout
  2. android:orientation="horizontal"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_gravity="bottom"
  6. >
  7. <com.notice.ib.ImageBt
  8. android:id="@+id/bt_confirm"
  9. android:layout_height="wrap_content"
  10. android:layout_width="wrap_content"
  11. android:layout_alignParentBottom="true"
  12. android:background="@drawable/btbg"
  13. android:clickable="true"
  14. android:focusable="true"
  15. />
  16. <com.notice.ib.ImageBt
  17. android:id="@+id/bt_cancel"
  18. android:layout_toRightOf="@id/bt_confirm"
  19. android:layout_height="wrap_content"
  20. android:layout_width="wrap_content"
  21. android:layout_alignParentBottom="true"
  22. android:background="@drawable/btbg"
  23. android:clickable="true"
  24. android:focusable="true"
  25. />
  26. </RelativeLayout>

注意的是,控件标签使用完整的类名即可。为了给按钮一个点击效果,你需要给他一个selector背景,这里就不说了。

最后一步,即在activity中设置该控件的内容。当然,在xml中也可以设置,但是只能设置一个,当我们需要两次使用这样的控件,并且显示内容 不同时就不行了。在activity中设置也非常简单,我们在ImageBt这个类中已经写好了相应的方法,简单调用即可。代码如下:

  1. public class MainActivity extends Activity {
  2. private ImageBt ib1;
  3. private ImageBt ib2;
  4. /** Called when the activity is first created. */
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.login);
  9. ib1 = (ImageBt) findViewById(R.id.bt_confirm);
  10. ib2 = (ImageBt) findViewById(R.id.bt_cancel);
  11. ib1.setTextViewText("确定");
  12. ib1.setImageResource(R.drawable.confirm);
  13. ib2.setTextViewText("取消");
  14. ib2.setImageResource(R.drawable.cancel);
  15. ib1.setOnClickListener(new OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. //在这里可以实现点击事件
  19. }
  20. });
  21. }
  22. }

这样,一个带文字和图片的组合按钮控件就完成了。这样梳理一下,使用还是非常简单的。组合控件能做的事还非常多,主要是在类似上例中的ImageBt类中写好要使用的方法即可。

再来看一个组合控件,带删除按钮的EidtText。即在用户输入后,会出现删除按钮,点击即可取消用户输入。

定义方法和上例一样。首先写一个自定义控件的布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <EditText
  7. android:id="@+id/et"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:singleLine="true"
  11. />
  12. <ImageButton
  13. android:id="@+id/ib"
  14. android:visibility="gone"
  15. android:src="@drawable/menu_delete"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:background="#00000000"
  19. android:layout_alignRight="@+id/et" />
  20. </RelativeLayout>

实现输入框右侧带按钮效果,注意将按钮隐藏。然后写一个EditCancel类,实现删除用户输入功能。这里用到了TextWatch这个接口,监听输入框中的文字变化。使用也很简单,实现他的三个方法即可。看代码:

  1. package com.notice.ce;
  2. import android.content.Context;
  3. import android.text.Editable;
  4. import android.text.TextWatcher;
  5. import android.util.AttributeSet;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.ImageButton;
  10. import android.widget.LinearLayout;
  11. public class EditCancel extends LinearLayout implements EdtInterface {
  12. ImageButton ib;
  13. EditText et;
  14. public EditCancel(Context context) {
  15. super(context);
  16. }
  17. public EditCancel(Context context, AttributeSet attrs) {
  18. super(context, attrs);
  19. LayoutInflater.from(context).inflate(R.layout.custom_editview, this, true);
  20. init();
  21. }
  22. private void init() {
  23. ib = (ImageButton) findViewById(R.id.ib);
  24. et = (EditText) findViewById(R.id.et);
  25. et.addTextChangedListener(tw);// 为输入框绑定一个监听文字变化的监听器
  26. // 添加按钮点击事件
  27. ib.setOnClickListener(new OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. hideBtn();// 隐藏按钮
  31. et.setText("");// 设置输入框内容为空
  32. }
  33. });
  34. }
  35. // 当输入框状态改变时,会调用相应的方法
  36. TextWatcher tw = new TextWatcher() {
  37. @Override
  38. public void onTextChanged(CharSequence s, int start, int before, int count) {
  39. // TODO Auto-generated method stub
  40. }
  41. @Override
  42. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  43. // TODO Auto-generated method stub
  44. }
  45. // 在文字改变后调用
  46. @Override
  47. public void afterTextChanged(Editable s) {
  48. if (s.length() == 0) {
  49. hideBtn();// 隐藏按钮
  50. } else {
  51. showBtn();// 显示按钮
  52. }
  53. }
  54. };
  55. @Override
  56. public void hideBtn() {
  57. // 设置按钮不可见
  58. if (ib.isShown()) ib.setVisibility(View.GONE);
  59. }
  60. @Override
  61. public void showBtn() {
  62. // 设置按钮可见
  63. if (!ib.isShown()) ib.setVisibility(View.VISIBLE);
  64. }
  65. }
  66. interface EdtInterface {
  67. public void hideBtn();
  68. public void showBtn();
  69. }

在TextWatch接口的afterTextChanged方法中对文字进行判断,若长度为0,就隐藏按钮,否则,显示按钮。

另外,实现ImageButton(即那个叉)的点击事件,删除输入框中的内容,并隐藏按钮。

后面两步的实现就是加入到实际布局中,就不再写出来了,和上例的步骤一样的。最后显示效果如图:

2011080723021151.png

学会灵活的使用组合控件,对UI开发会有很大帮助。

Android 自定义控件外观

首先我们看下系统的RadioButton:
RadioButton长成什么样子是由其Background、Button等属性决定的,Android系统
使用style定义了默认的属性,在android源码
android/frameworks/base/core/res/res/values/styles.xml中可以看到默认的定义:

  1. <style name="Widget.CompoundButton.RadioButton">
  2. <item name="android:background">@android:drawable/btn_radio_label_background</item>
  3. <item name="android:button">@android:drawable/btn_radio</item>
  4. </style>

即其背景图是btn_radio_label_background,其button的样子是btn_radio

btn_radio_label_background是什么?
其路径是android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_label_background.9.png
可以看到是一个NinePatch图片,用来做背景,可以拉伸填充。

btn_radio是什么?
其路径是android/frameworks/base/core/res/res/drawable/btn_radio.xml
是个xml定义的drawable,打开看其内容:

  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item android:state_checked="true" android:state_window_focused="false"
  3. android:drawable="@drawable/btn_radio_on" />
  4. <item android:state_checked="false" android:state_window_focused="false"
  5. android:drawable="@drawable/btn_radio_off" />
  6. <item android:state_checked="true" android:state_pressed="true"
  7. android:drawable="@drawable/btn_radio_on_pressed" />
  8. <item android:state_checked="false" android:state_pressed="true"
  9. android:drawable="@drawable/btn_radio_off_pressed" />
  10. <item android:state_checked="true" android:state_focused="true"
  11. android:drawable="@drawable/btn_radio_on_selected" />
  12. <item android:state_checked="false" android:state_focused="true"
  13. android:drawable="@drawable/btn_radio_off_selected" />
  14. <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
  15. <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
  16. </selector>

定义了不同状态下radioButton长成什么样子。
如果不知道selector是什么,就要去看下Android SDK文档中Dev Guide->Application Resources->Resource Types。
以下面一个item为例:


意思即为当radiobutton被选中时,并且被按下时,其Button应该长成btn_radio_on_pressed这个样子。
a723f9b8-4779-35c8-8a38-810cee6b2d39.png
文件是android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_on_pressed.png

drawable的item中可以有以下属性:
android:drawable=”@[package:]drawable/drawable_resource”
android:state_pressed=[“true” | “false”]
android:state_focused=[“true” | “false”]
android:state_selected=[“true” | “false”]
android:state_active=[“true” | “false”]
android:state_checkable=[“true” | “false”]
android:state_checked=[“true” | “false”]
android:state_enabled=[“true” | “false”]
android:state_window_focused=[“true” | “false”]
当按钮的状态和某个item匹配后,就会使用此item定义的drawable作为按钮图片。

从上面分析我们如果要修改RadioButton的外观,那么步骤应该是:
(1)制作一个9patch的图片作为背景图
准备一副PNG图片,其中白色为透明色,是否需要透明各人根据自己需要决定。
6f1cef00-7a0d-36ca-b764-cfa0abbd893c.png
运行SDK/tools/draw9patch
在可伸缩的范围周围加上黑色的线告知系统这些区域可以伸缩。
制作完的图片,周围多了黑色线。
a1634e32-d358-3ca3-9155-07198e4dca25.png
(2)针对不同的状态提供按钮图片
enabled, on: 紫色外框、红色中心点
f206b1ed-352a-357d-9115-61c55b734db5.png
enabled, off:只有紫色外框
859d3327-a788-3e92-96be-034daa4f1cc5.png
enabled, on, pressed:黄色外框,红色中心点
89364572-70eb-37e7-9d12-6c273a98ea60.png
enabled, off, pressed:黄色外框
172a57da-37af-3a32-9c76-51ef416015a9.png
disabled, on: 灰色外框、灰色中心点
f93b9358-1a56-37e3-b46e-a69c1144d1bc.png
disabled, off: 灰色外框
066690a5-8ca9-3913-83e9-0c56b9b5ffd3.png
其余的状态此处就不再定义。
(3)使用xml描述一个drawable
在res/drawable/创建custom_radio_btn.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_enabled="true" android:state_checked="true" android:state_pressed="true"
  4. android:drawable="@drawable/enabled_on_pressed" />
  5. <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true"
  6. android:drawable="@drawable/enabled_off_pressed" />
  7. <item android:state_enabled="true" android:state_checked="true"
  8. android:drawable="@drawable/enabled_on" />
  9. <item android:state_enabled="true" android:state_checked="false"
  10. android:drawable="@drawable/enabled_off" />
  11. <item android:state_enabled="false" android:state_checked="true"
  12. android:drawable="@drawable/disabled_on" />
  13. <item android:state_enabled="false" android:state_checked="false"
  14. android:drawable="@drawable/disabled_off" />
  15. </selector>

Item顺序是有讲究的,条件限定越细致,则应该放到前面。比如这儿如果把1,2行和3,4行的item交换,那么pressed的就永远无法触发了,因为有item已经满足条件返回了。可以理解为代码中的if语句。
(4)创建一个自定义的style,并应用到RaidioButton的style属性上

  1. <style name="CustomRadioBtn">
  2. <item name="android:background">@drawable/radio_btn_bg</item>
  3. <item name="android:button">@drawable/custom_radio_btn</item>
  4. </style>

运行ap即可看到此RadioButton的外观已经改变,此demo可以看到文字被按钮遮盖了一部分,
这儿是因第一步制作9patch图片时没有留出按钮图片空间来,稍作修改即可。
2e41ae61-2073-3d6d-9800-1c7008d6171d.png

  1. fromhttp://mypyg.iteye.com/blog/768471

转载于:https://www.cnblogs.com/bill-joy/archive/2012/04/26/2471831.html

发表评论

表情:
评论列表 (有 0 条评论,656人围观)

还没有评论,来说两句吧...

相关阅读

    相关 android 定义

    Android自定义View实现很简单 继承View,重写构造函数、onDraw,(onMeasure)等函数。 如果自定义的View需要有自定义的属性,需要在values