自定义按钮
1》定义按钮布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/iconMoney"
android:layout_width="25dp"
android:layout_height="fill_parent"
>
</ImageView>
<TextView
android:id="@+id/numMeoney"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:text="4444"
android:textSize="20dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="8dp"
>
</TextView>
<ImageView
android:layout_marginLeft="5dp"
android:id="@+id/iconAdd"
android:layout_width="25dp"
android:layout_height="fill_parent"
>
</ImageView>
</LinearLayout>
2》继承布局文件
package com.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.dreamplanegames.R;
public class MoneyView extends LinearLayout {
private ImageView iconMoney;
private TextView textView;
private ImageView Addmoney;
public MoneyView(Context context,AttributeSet attributeSet) {
super(context, attributeSet);
LayoutInflater.from(context).inflate(R.layout.money, this,true);//指定布局
this.iconMoney = (ImageView)findViewById(R.id.iconMoney);
this.textView = (TextView)findViewById(R.id.numMeoney);
this.Addmoney=(ImageView)findViewById(R.id.iconAdd);
this.setClickable(true);//可以点击
this.setFocusable(true);
}
//设置控件内容
public void setText(String text) {
this.textView.setText(text);
}
public void setTextColor(int color) {
this.textView.setTextColor(color);
}
public void setTextSize(float size) {
this.textView.setTextSize(size);
}
public void setImg(Bitmap img1,Bitmap img2) {
this.iconMoney.setImageBitmap(img1);
this.Addmoney.setImageBitmap(img2);
//this.yes.setImageBitmap(img2);
}
}
3》控件的调用
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView android:id="@+id/mainMenuImage"
android:src="@drawable/xk_bg2"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
//调用自定义控件
<com.widget.MoneyView--------------------类名
android:layout_width="wrap_content"
android:layout_height="30dp"
android:id="@+id/btnMoney"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
>
</com.widget.MoneyView>
</RelativeLayout>
4》到相应的activity调用
public class SFMainMenu extends Activity implements View.OnClickListener
{
public MoneyView moneyview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//去除应用程序标题
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);
//获取自定义按钮
moneyview=(MoneyView)findViewById(R.id.btnMoney);
//调用自定义控件的函数设置控件内容
moneyview.setImg(BitmapFactory.decodeResource(getResources(), R.drawable.money),BitmapFactory.decodeResource(getResources(), R.drawable.add));
moneyview.setText(""+myPointBalance);
//自定义按钮响应事件
moneyview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
//返回键
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
相关推荐
Android自定义控件开发入门与实战从自定义基础到实战的讲解。一步步深入。适合有一定Android基础的读者。本压缩包中自带了推荐的pdf阅读器。大家要是喜欢这本文档,推荐去京东,天猫,当当买支持一下默默付出的作者...
《Android自定义控件开发入门与实战》高清影印版,带目录,拒绝压缩包加密坑钱,内容深入浅出,对于有Android自定义开发需求的非常有帮助
1 本书从动画、绘图、视图三方面介绍Android自定义控件相关知识,内容系统全面,并配以翔实的案例。 2 Android自定义控件涉及动画和色彩,本书将图片地址制作成二维码,可供读者扫描观看。 3 本书适合初高级水平从业...
Android自定义控件示例,基于Android studio 1.0.2。
Android自定义控件详细开发教程
Android 自定义控件简单Demo 图片+文字
Android自定义控件步骤总结 http://www.cnblogs.com/duanxz/p/3625589.html
Android自定义控件源码含APK 仿Material Design风格,在低版本android环境上面实现高版本中的按钮等UI控件,视觉效果提升,本源码将向你介绍一些方法,实现这种效果。注:本源码中使用的控件是原作者已经封装好的,...
《Android自定义控件入门到实战》源码
android自定义控件
这是我写的 Android 自定义控件入门 这个小教程的例子源码,十分基础,用了同一个例子的不同写法和扩展,简单明了的讲解了View的重写,教程请去我的博客,地址为:...希望大家能通过简单的例子学到东西,举一反三。...
Android 组合控件,用自定义方式实现,非简单重写adapter。同时包含了点击事件的响应。
android 自定义控件 小球的圆周运动 canvas绘出
详细讲解自定义控件文章的配套代码!文章地址:http://blog.csdn.net/dinglin_87/article/details/7431545
Android自定义控件源码.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
教程名称: 尚硅谷Android视频教程《自定义控件》Android自定义控件,是中高级程序员必须熟练掌握的技术之一。本套视频涵盖了安卓自定义开发过程中所有的技术问题,课程中讲授的例子全部来源于企业。学习本套视频后...
Android自定义控件之滑动开关 Android自定义控件之滑动开关 http://blog.csdn.net/eeeduo/article/details/37606567
android自定义控件CircleImageVIew的使用举例
这个资源是工作中使用到的自定义控件及一些开源框架收集,挺不错的。
android自定义控件LinkTextView实现