- 浏览: 363073 次
- 性别:
- 来自: 南京
-
文章分类
最新评论
-
nglxl:
点赞啊,必须点赞!!深度好文,解决了困扰我多年的疑惑。
tcp 长连接与短连接 -
cofftech:
opengl源码http://www.eyesourcecod ...
OPenGL ES 关键API小结 -
mail_j:
不是很好用,很多情况都不能处理 1、没有区分关键字的大小写2、 ...
Java解析sql语句,分析出调用到的所有表 -
a455642158:
就算监听到变成了cmwap还不行,还得监听网络连接状态Stat ...
android APN切换cmwap实现 -
xuhl1022:
看了您的android 4篇 浏览器二次开发,感触颇深,写的很 ...
Android浏览器Browser二次开发(四)浏览器中的APN切换
实现自定义Button有两种方式,
1. 继承View,在里面自己去实现onDraw(), onMeasure(), onClickListener()等方法。这种方式比较灵活,可以实现复杂的需求。
代码样例如下:
package seya.test.comp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class CustomButton extends View{
private final static int WIDTH_PADDING = 8;
private final static int HEIGHT_PADDING = 10;
private final String label;
private final int imageResId;
private final Bitmap image;
//private final InternalListener listenerAdapter = new InternalListener();
public CustomButton(Context context, int resImage, String label)
{
super(context);
this.label = label;
this.imageResId = resImage;
this.image = BitmapFactory.decodeResource(context.getResources(),
imageResId);
setFocusable(true);
setBackgroundColor(Color.WHITE);
//setOnClickListener(listenerAdapter);
setClickable(true);
}
@Override
protected void onFocusChanged (boolean gainFocus, int direction, Rect previouslyFocusedRect){
this.setBackgroundColor(Color.GREEN);
}
@Override
protected void onDraw(Canvas canvas)
{
Paint textPaint = new Paint();
textPaint.setColor(Color.BLACK);
canvas.drawBitmap(image, WIDTH_PADDING / 2, HEIGHT_PADDING / 2, null);
canvas.drawText(label, WIDTH_PADDING / 2, (HEIGHT_PADDING / 2) +
image.getHeight() + 8, textPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec)
{
int preferred = image.getWidth() * 2;
return getMeasurement(measureSpec, preferred);
}
private int measureHeight(int measureSpec)
{
int preferred = image.getHeight() * 2;
return getMeasurement(measureSpec, preferred);
}
private int getMeasurement(int measureSpec, int preferred)
{
int specSize = MeasureSpec.getSize(measureSpec);
int measurement = 0;
switch(MeasureSpec.getMode(measureSpec))
{
case MeasureSpec.EXACTLY:
// This means the width of this view has been given.
measurement = specSize;
break;
case MeasureSpec.AT_MOST:
// Take the minimum of the preferred size and what
// we were told to be.
measurement = Math.min(preferred, specSize);
break;
default:
measurement = preferred;
break;
}
return measurement;
}
/* public void setOnClickListener(ClickListener newListener)
{
listenerAdapter.setListener(newListener);
}*/
public String getLabel()
{
return label;
}
/**
* Returns the resource id of the image.
*/
public int getImageResId()
{
return imageResId;
}
/* private class InternalListener implements View.OnClickListener
{
private ClickListener listener = null;
public void setListener(ClickListener newListener)
{
listener = newListener;
}
@Override
public void onClick(View v)
{
if (listener != null)
{
listener.onClick(CustomImageButton.this);
}
}
}*/
}
2. 另一种还是保持原来button的基本功能,只需要改变button在不同状态下的样式,那么就可以这样来做:
在res/drawable下面新建mybutton_background.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/yellow" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/green" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/blue" />
<item android:drawable="@drawable/grey" />
</selector>
这里面就定义了在不同状态下的显示图片
然后在layout里面定义Button的时候,指定它的background为这个mybutton_background
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mybtn"
android:background="@drawable/mybutton_background"/>
</LinearLayout>
这种方式开发比较简单,适合做一些风格一致的Button,设置成同一个background就可以了。
1. 继承View,在里面自己去实现onDraw(), onMeasure(), onClickListener()等方法。这种方式比较灵活,可以实现复杂的需求。
代码样例如下:
package seya.test.comp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class CustomButton extends View{
private final static int WIDTH_PADDING = 8;
private final static int HEIGHT_PADDING = 10;
private final String label;
private final int imageResId;
private final Bitmap image;
//private final InternalListener listenerAdapter = new InternalListener();
public CustomButton(Context context, int resImage, String label)
{
super(context);
this.label = label;
this.imageResId = resImage;
this.image = BitmapFactory.decodeResource(context.getResources(),
imageResId);
setFocusable(true);
setBackgroundColor(Color.WHITE);
//setOnClickListener(listenerAdapter);
setClickable(true);
}
@Override
protected void onFocusChanged (boolean gainFocus, int direction, Rect previouslyFocusedRect){
this.setBackgroundColor(Color.GREEN);
}
@Override
protected void onDraw(Canvas canvas)
{
Paint textPaint = new Paint();
textPaint.setColor(Color.BLACK);
canvas.drawBitmap(image, WIDTH_PADDING / 2, HEIGHT_PADDING / 2, null);
canvas.drawText(label, WIDTH_PADDING / 2, (HEIGHT_PADDING / 2) +
image.getHeight() + 8, textPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec)
{
int preferred = image.getWidth() * 2;
return getMeasurement(measureSpec, preferred);
}
private int measureHeight(int measureSpec)
{
int preferred = image.getHeight() * 2;
return getMeasurement(measureSpec, preferred);
}
private int getMeasurement(int measureSpec, int preferred)
{
int specSize = MeasureSpec.getSize(measureSpec);
int measurement = 0;
switch(MeasureSpec.getMode(measureSpec))
{
case MeasureSpec.EXACTLY:
// This means the width of this view has been given.
measurement = specSize;
break;
case MeasureSpec.AT_MOST:
// Take the minimum of the preferred size and what
// we were told to be.
measurement = Math.min(preferred, specSize);
break;
default:
measurement = preferred;
break;
}
return measurement;
}
/* public void setOnClickListener(ClickListener newListener)
{
listenerAdapter.setListener(newListener);
}*/
public String getLabel()
{
return label;
}
/**
* Returns the resource id of the image.
*/
public int getImageResId()
{
return imageResId;
}
/* private class InternalListener implements View.OnClickListener
{
private ClickListener listener = null;
public void setListener(ClickListener newListener)
{
listener = newListener;
}
@Override
public void onClick(View v)
{
if (listener != null)
{
listener.onClick(CustomImageButton.this);
}
}
}*/
}
2. 另一种还是保持原来button的基本功能,只需要改变button在不同状态下的样式,那么就可以这样来做:
在res/drawable下面新建mybutton_background.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/yellow" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/green" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/blue" />
<item android:drawable="@drawable/grey" />
</selector>
这里面就定义了在不同状态下的显示图片
然后在layout里面定义Button的时候,指定它的background为这个mybutton_background
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mybtn"
android:background="@drawable/mybutton_background"/>
</LinearLayout>
这种方式开发比较简单,适合做一些风格一致的Button,设置成同一个background就可以了。
发表评论
-
基于Java NIO的手机答题游戏开发
2014-03-31 17:59 1806先上个游戏截图: 豌豆荚地址: http://apps.w ... -
enable android deviceconnection under linux
2013-04-24 15:45 1015开发环境搭建完毕,Eclipse,Java,ADT都已经折腾 ... -
set CCache for building android source code
2013-04-24 15:14 1199Setting up ccache You can opti ... -
android 源码下载403 forbidden, 406 not acceptable 错误
2013-04-24 08:46 2456解决方法 1. 浏览器登录https://android.g ... -
ubuntu11.10 编译android4.0 错处
2013-04-08 21:45 1232今天使用ubuntu11.10编译android4.0出现以下 ... -
常用WebService列表
2013-01-31 23:22 2647快递查询接口 http://webservice.36wu.c ... -
startActivityForResult 无响应问题
2013-01-24 13:57 3879今天开发遇到了个问题, 就是从一个Activity中使用sta ... -
Android中仿新浪微博刷新列表
2012-11-22 17:35 7350大家看到新浪微博中的列表往下拉的时候, 头部出现正在刷新,然后 ... -
Grid调整间距
2012-09-29 08:52 1757今天使用GridView, 发现点中某一Item时选中部分的背 ... -
android gallery和Animation组合使用, 看美女美图
2012-09-11 21:01 4462今天主要探究Gallery和Animation的使用。 制作一 ... -
Android执行 shell command
2012-03-24 17:00 7278Android执行shell命令 一、方法 1. /* ... -
BBBBBB111
2012-03-23 11:42 10陈波: 本周:代码review,解决findbugs中bug以 ... -
MonkeyTestError
2012-03-21 10:15 903-20 21:36:42.439 W/dalvikvm( ... -
INSTALL_FAILED_OLDER_SDK ERROR
2012-03-12 15:52 4488Install APK with adb: $ platfo ... -
4.0源码编译问题
2012-03-07 17:12 31661、fatal error: GL/glx.h: No suc ... -
4.0 编译apk中无classes.dex
2012-03-07 17:11 3985下载完android 4.0代码,模拟器里面的gallery不 ... -
Android 4.0源码编译错误
2012-03-05 10:19 2591UNEXPECTED TOP-LEVEL EXCEPTION: ... -
AndroidLockScreenDemo
2012-02-18 15:54 1008锁屏解锁的成功案例。 -
G14 root权限获取
2012-02-13 23:36 2875HTC G14 ROOT权限获取后就能删除系统自带的程序,相信 ... -
锁屏d ds
2011-12-15 00:49 919private final IDevicePolicyMana ...
相关推荐
在本项目"ios-CustomButton.zip"中,开发者已经创建了一个自定义的按钮类,实现了图片位于上方、文字位于下方,并且具有下划线效果的两种按钮样式。这种自定义的按钮可以为应用的UI提供更多的设计可能性,使其更符合...
"CustomButton.rar" 提供了一个在UE4.22版本中的插件,专门用于创建非标准形状的按钮,以满足游戏设计中的独特需求。这个插件允许开发者突破系统默认按钮的限制,设计出更符合游戏风格或交互体验的UI元素。 首先,...
compile ' com.github.whilu.CustomButton:library:1.0.0 ' } 如果它不起作用,请给我。 第2步 首先,在 XML 文件中添加命名空间: xmlns:whilu="http://schemas.android.com/apk/res-auto" 然后,您可以轻松使用...
自定义按钮 ...import CustomButton @main final class AppDelegate : NSObject , NSApplicationDelegate { @IBOutlet weak var window: NSWindow ! func applicationDidFinishLaunching ( _ notificati
我们可以使用`document.getElementById('customButton').getContext('2d')`来获取到这个对象。 在Canvas上绘制按钮,我们首先定义按钮的基本形状。通常,一个按钮可能由矩形、圆角矩形或者更复杂的形状构成。以下是...
【Droid-CustomButton】项目是一个专为Android平台设计的示例工程,它展示了如何在不依赖XML资源或额外图像的情况下实现自定义按钮。这个项目对于开发者来说,是一次了解和学习Android UI自定义的好机会,特别是对于...
CustomButton *myButton = new CustomButton(this); connect(myButton, &CustomButton::clicked, this, [this](){ customButtonClickAction(); }); ``` 5. **资源文件和样式表** 如果你想要更复杂的样式,可以...
这些图片可以通过设置`CustomButton.Glyph`属性来应用到按钮上,以改变其默认样式。例如,你可以指定一个按下状态的图像,当用户鼠标悬停或点击按钮时显示。 最后,DLL文件可能包含额外的函数库,用于扩展Inno ...
public class CustomButton extends Button { // 在这里添加新的方法或重写父类的方法 } ``` 接下来,我们讨论如何为自定义控件添加自定义属性。Android提供了XML资源文件来定义控件的属性。自定义属性需要在res/...
public class CustomButton extends AppCompatButton { private int defaultResId; private int pressedResId; private int focusedResId; private int unfocusedResId; public CustomButton(Context context)...
CustomButton customButton = findViewById(R.id.custom_button); customButton.setImageResource(R.drawable.my_image); customButton.setText("点击我"); ``` 通过以上步骤,我们就成功地创建并使用了一个自定义...
CustomButton *myButton = new CustomButton(parentWidget); myButton->setText("点击我"); // 设置按钮文字 ``` 通过这种方式,你可以实现Qt按钮的文字和图片分开显示,提高界面的可读性和美观度。还可以根据需要...
`obtainStyledAttributes()` 方法用于创建TypedArray对象,传入的第二个参数是R.styleable.CustomButton,这是我们在attr.xml中定义的styleable资源ID。 四、自定义属性与主题(Theme) 除了在布局文件中直接设置...
borderColor = a.getColor(R.styleable.CustomButton_borderColor, Color.TRANSPARENT); borderWidth = a.getDimension(R.styleable.CustomButton_borderWidth, 0); animationDuration = a.getInteger(R....
let customButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50)) ``` 2. **设置按钮类型**:`UIButton`有多种类型,例如`.system`、`.custom`等,可以通过`buttonType`属性设置。 ```swift ...
例如,可以添加一个名为`customTextColor`的属性,以便在代码中通过`getAttributes().getColorStateList(R.styleable.CustomButton_customTextColor)`获取并设置按钮文字颜色。 5. **重写`onDraw()`方法**: 如果...
CustomButton customButton = findViewById(R.id.custom_button); customButton.setOnBackgroundChangeListener(new BackgroundChangeListener() { @Override public void onBackgroundChanged(int ...