三九宝宝网宝宝百科宝宝知识

android自定义控件怎么用

03月08日 编辑 39baobao.com

[自定义Excel打印页面自定义Excel3]设定你的打印页面对于办公一族来说,打印可以说是家常便饭了,但你是否细细地看了下菜单文件中,页面设定里的每一项功能,又是否能给每个功能想一个应用的例子?下面我将讲述这里面...+阅读

一、控件自定义属性介绍 以下示例中代码均在values/attrs.xml 中定义,属性均可随意命名。 1. reference:参考某一资源ID。 示例:2. color:颜色值。 示例:3. boolean:布尔值。 示例:4. dimension:尺寸值。 示例:5. float:浮点值。 示例:6. integer:整型值。 示例:7. string:字符串。 示例:8. fraction:百分数。 示例:9. enum:枚举值。 示例:10. flag:位或运算。

示例:11.多类型。 示例:

二、属性的使用以及自定义控件的实现

1、构思控件的组成元素,思考所需自定义的属性。 比如:我要做一个(类似9宫格按钮) 新建values/attrs.xml以上,所定义为custom_view,custom_id为按钮id,src为按钮,background为阴影背景,text为按钮说明,textColor为字体颜色,textSize为字体大小。

2、怎么自定义控件呢,怎么使用这些属性呢?话不多说请看代码,CustomView : package com.nanlus.custom; import com.nanlus.custom.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; public class CustomView extends FrameLayout implements OnClickListener { private CustomListener customListener = null; private Drawable mSrc = null, mBackground = null; private String mText = ""; private int mTextColor = 0; private float mTextSize = 20; private int mCustomId = 0; private ImageView mBackgroundView = null; private ImageButton mButtonView = null; private TextView mTextView = null; private LayoutParams mParams = null; public CustomView(Context context) { super(context); } public CustomView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.custom_view); mSrc = a.getDrawable(R.styleable.custom_view_src); mBackground = a.getDrawable(R.styleable.custom_view_background); mText = a.getString(R.styleable.custom_view_text); mTextColor = a.getColor(R.styleable.custom_view_textColor, Color.WHITE); mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20); mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0); mTextView = new TextView(context); mTextView.setTextSize(mTextSize); mTextView.setTextColor(mTextColor); mTextView....

android中怎样实现自定义控件中的组合控件

public class MyView extends View{

//此处省略构造方法

private void onDraw(Canvas canvas){

//重写view的onDraw方法,绘制控件的样式

//这里你使用canvas来绘制,你布局中使用这个控件就是你绘制的样子

}

//然后你可以定义很多自己的一些方法,用来修改控件的样式

//假如你自定义的一个进度条的话,就要修改进度条值,你就可以自定义方法,让实现对象来改变进度值,记得修改后调用validate方法更新显示。(具体函数记不太清了)

}

大概就是这样实现的自定义控件,自定义控件的话优化是很重要的哦,不然性能会很差。

然后你要使用这个控件的话,在布局中就需要这样定义,假如这个自定义控件类是这样的:

xxx.xxx.MyView。

则使用时:

这些地方一样的设置宽高,id啊杂七杂八的属性

/>

如何打造Android自定义的下拉列表框控件

实现方式:

1、水平布局一个TextView和一个ImageView(小黑箭头)

2、实现点击ImageView的单击事件,弹出PopupWindow

3、PopupWindow中实现下拉列表

关键代码示例:

1、布局

android:layout_height="match_parent"

android:orientation="horizontal">

2、单击事件

image.setBackgroundResource(R.drawable.gerendang_jiantou);

image.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

//弹出popupwindow

}

});

3、pupupwindow相关代码

ListView lv = new ListView(this);

adapter = new OptionsAdapter(context, datas); // 根据数据,设置下拉框显示

list.setAdapter(adapter);

/**

* 两种不同长度的下拉框,主要是为了适应屏幕的大小

*/

if (p_width >0) {

pWindow = new PopupWindow(v, par.getWidth(), 150);

} else {

pWindow = new PopupWindow(v, par.getWidth(), 300);

}

pWindow.setFocusable(true); //能...实现方式:

1、水平布局一个TextView和一个ImageView(小黑箭头)

2、实现点击ImageView的单击事件,弹出PopupWindow

3、PopupWindow中实现下拉列表

关键代码示例:

1、布局

android:layout_height="match_parent"

android:orientation="horizontal">

2、单击事件

image.setBackgroundResource(R.drawable.gerendang_jiantou);

image.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

//弹出popupwindow

}

});

3、pupupwindow相关代码

ListView lv = new ListView(this);

adapter = new OptionsAdapter(context, datas); // 根据数据,设置下拉框显示

list.setAdapter(adapter);

/**

* 两种不同长度的下拉框,主要是为了适应屏幕的大小

*/

if (p_width >0) {

pWindow = new PopupWindow(v, par.getWidth(), 150);

} else {

pWindow = new PopupWindow(v, par.getWidth(), 300);

}

pWindow.setFocusable(true); //能够焦点获得

pWindow.setBackgroundDrawable(new BitmapDrawable()); //设置背景

pWindow.setOutsideTouchable(true); //外部点击关闭

pWindow.update(); //更新位置

android自定义控件我想做一个自定义相对布局控件中包含两个子

你可以get父控件的layoutparams,然后取到里面的高,然后通过这个高,来定义一个layoutparams, set给你的textView便得了,我写的方法给你参考下:/** * 将传进来view的布局参数按照比例缩放,以适应不同的屏幕大小,这里处理了RelativeLayout、FrameLayout、 * LinearLayout三种容器里View的布局参数,如有需要可自行增减,其中的scaleWidth=现设备宽/原设备宽 * scaleHeight = 现设备高/原设备高,这个需要用displaymetrics类来取 * param view * param scaleWidth * param scaleHeight */ public static void setParams(View view, float scaleWidth, float scaleHeight) { RelativeLayout.LayoutParams rlParams = null; FrameLayout.LayoutParams flParams = null; LinearLayout.LayoutParams llParams = null; if (view.getParent() instanceof RelativeLayout) { rlParams = (RelativeLayout.LayoutParams) (view.getLayoutParams()); rlParams.width = (int) (rlParams.width * scaleWidth); rlParams.height = (int) (rlParams.height * scaleHeight); rlParams.leftMargin = (int) (rlParams.leftMargin * scaleWidth); rlParams.rightMargin = (int) (rlParams.rightMargin * scaleWidth); rlParams.topMargin = (int) (rlParams.topMargin * scaleHeight); rlParams.bottomMargin = (int) (rlParams.bottomMargin * scaleHeight); view.setLayoutParams(rlParams); } else if (view.getParent() instanceof FrameLayout) { flParams = (FrameLayout.LayoutParams) (view.getLayoutParams()); flParams.width = (int) (flParams.width * scaleWidth); flParams.height = (int) (flParams.height * scaleHeight); flParams.leftMargin = (int) (flParams.leftMargin * scaleWidth); flParams.rightMargin = (int) (flParams.rightMargin * scaleWidth); flParams.topMargin = (int) (flParams.topMargin * scaleHeight); flParams.bottomMargin = (int) (flParams.bottomMargin * scaleHeight); view.setLayoutParams(flParams); } else if (view.getParent() instanceof LinearLayout) { llParams = (LinearLayout.LayoutParams) (view.getLayoutParams()); llParams.width = (int) (llParams.width * scaleWidth); llParams.height = (int) (llParams.height * scaleHeight); llParams.leftMargin = (int) (llParams.leftMargin * scaleWidth); llParams.rightMargin = (int) (llParams.rightMargin * scaleWidth); llParams.topMargin = (int) (llParams.topMargin * scaleHeight); llParams.bottomMargin = (int) (llParams.bottomMargin * scaleHeight); view.setLayoutParams(llParams); } }

以下为关联文档:

定义单元格格式自定义Excel定义格式:自定义格式主要说两点:1、创建或删除自定义数字格式,2、条件格式,因为这一部分在Excel的帮助中的解释已非常清楚,就将帮助的内容复制于此创建自定义数字格式选择...

定义序列、查找与替换和批注自定义Excel定义序列:对于填充的功能,大家可能都了解,但有一些表面看上去可以填充的内容,却无法真正自动填充。比如:一年级、二年级……六年级。这里,就可以用自定义序列来现实!在菜单工具/...

请教android怎么让控件背景透明Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... /> 透明<Button android:background="#00000000" ... /> 颜色和不透明度 (a...

动态添加Android安卓控件动态添加Android(安卓)控件步骤: 1、addView 添加控件到布局容器 2、removeView 在布局容器中删掉已有的控件 3、使用 public class MainActivity extends Activity { Override...

android自定义控件怎么实现动态绘制代码: public class MyView extends View { //坐标轴原点的位置 private int xPoint=60; private int yPoint=260; //刻度长度 private int xScale=8; //8个单位构成一个刻度...

求解:android中如何实现动态插入控件直接给你上代码吧,写了我半个小时,经过了我的测试了的~运行下就能看到结果了~关键的remove的时候有给你写注释~布局的layout文件内容:---------------------------------------...

什么是自定义放映 ppt怎么进行自定义放映定义放映是指: 若用户并不希望将演示文稿的所有部分展现给观众,而是需要根据不同的观众选择不同的放映部分,可以根据需要自主定义放映部分。 1、在PPT2010中打开已经制作好的...

VB控件数组怎么定义你的过程名称不是数组控件的过程,你新建控件,复制黏贴就能弹出是不是要建立控件数组的提示,这就能建立控件数组,双击控件得到下面的过程名: Private Sub Check1_Click(Index As I...

这个自定义函数是什么意思干什么用的LenB函数是返回字符串的字节总数; StrConv函数是返回按指定类型转换的Variant(String);vbFromUnicode表示将字符串由Unicode转成系统的缺省码页; 上面这个自定义函数是返回字...

推荐阅读
图文推荐