当前位置:首页 » 安卓系统 » android自定控件

android自定控件

发布时间: 2022-09-03 21:10:09

① 如何在android程序中,动态的增加自定控件

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup vg = new LinearLayout(this);//只要得到一个ViewGroup对象就可以了,一般来说是布局view
setContentView(vg); //设置
for(int i = 0 ; i< 5; i++){
TextView tv = new TextView(this);
tv.setText("hahahah");
addView(vg,tv);
}
}

private void addView(ViewGroup vg, View view){//添加View
if(null == vg || null == view)
return;
vg.addView(view);
}

② android自定义控件怎么用

一、控件自定义属性介绍
以下示例中代码均在values/attrs.xml 中定义,属性均可随意命名。
1. reference:参考某一资源ID。
示例:
<declare-styleable name = "名称">

<attr name = "background" format = "reference" />
<attr name = "src" format = "reference" />
</declare-styleable>

2. color:颜色值。
示例:
<declare-styleable name = "名称">

<attr name = "textColor" format = "color" />
</declare-styleable>

3. boolean:布尔值。
示例:
<declare-styleable name = "名称">

<attr name = "focusable" format = "boolean" />
</declare-styleable>

4. dimension:尺寸值。
示例:
<declare-styleable name = "名称">

<attr name = "layout_width" format = "dimension" />
</declare-styleable>

5. float:浮点值。
示例:
<declare-styleable name = "名称">

<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>

6. integer:整型值。
示例:
<declare-styleable name = "名称">

<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
</declare-styleable>

7. string:字符串。
示例:
<declare-styleable name = "名称">

<attr name = "text" format = "string" />
</declare-styleable>

8. fraction:百分数。
示例:

<declare-styleable name="名称">
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
</declare-styleable>

9. enum:枚举值。
示例:
<declare-styleable name="名称">

<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>

10. flag:位或运算。
示例:
<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" />
</attr>
</declare-styleable>

11.多类型。
示例:

<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>

二、属性的使用以及自定义控件的实现
1、构思控件的组成元素,思考所需自定义的属性。
比如:我要做一个 <带阴影的按钮,按钮正下方有文字说明>(类似9宫格按钮)
新建values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="custom_view">
<attr name="custom_id" format="integer" />
<attr name="src" format="reference" />
<attr name="background" format="reference" />
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>

以上,所定义为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.setText(mText);
mTextView.setGravity(Gravity.CENTER);
mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView = new ImageButton(context);
mButtonView.setImageDrawable(mSrc);
mButtonView.setBackgroundDrawable(null);
mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView.setOnClickListener(this);
mBackgroundView = new ImageView(context);
mBackgroundView.setImageDrawable(mBackground);
mBackgroundView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(mBackgroundView);
addView(mButtonView);
addView(mTextView);
this.setOnClickListener(this);
a.recycle();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mParams = (LayoutParams) mButtonView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mButtonView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mBackgroundView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mBackgroundView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mTextView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
mTextView.setLayoutParams(mParams);
}
}
public void setCustomListener(CustomListener l) {
customListener = l;
}
@Override
public void onClick(View v) {
if (customListener != null) {
customListener.onCuscomClick(v, mCustomId);
}
}
public interface CustomListener {
void onCuscomClick(View v, int custom_id);
}
}
代码很简单,就不多说,下面来看看我们的CustomView是怎么用的,请看:

3、自定义控件的使用
话不多说,请看代码,main.xml:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<com.nanlus.custom.CustomView
android:id="@+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
nanlus:background="@drawable/background"
nanlus:custom_id="1"
nanlus:src="@drawable/style_button"
nanlus:text="按钮1" >
</com.nanlus.custom.CustomView>
</LinearLayout>
</RelativeLayout>

在这里需要解释一下,
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"
nanlus为在xml中的前缀,com.nanlus.custom为包名

4、在Activity中,直接上代码
package com.nanlus.custom;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nanlus.BaseActivity;
import com.nanlus.custom.R;
import com.nanlus.custom.CustomView.CustomListener;
public class CustomActivity extends BaseActivity implements CustomListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);
}
@Override
public void onCuscomClick(View v, int custom_id) {
switch (custom_id) {
case 1:
Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}

③ Android自定义控件CustomView,构造函数的参数context是怎么获取的

很明显,这个context是在调用构造函数的时候传递进来的。

以两个参数的构造函数为例,这个一般是在xml使用该控件后,解析xml时会调用构造方法。

那么这个时候传的context怎么来的呢?从父类传过来的,父类呢,也是从父类的父类传过来的,顶层父类是Decorview,那么看看Decorview的context怎么来的。

上图ActivityThread.java里的一个方法。

创建activity实例之前,我们会先创建context,而这个context实际上就是new 了一个ContextImpl。然后和activity绑定。所以getContext实际上就是一个ContextImpl实例。

④ android studio自定义的控件怎么使用

首先你要自定义一个控件,一般采用继承原有控件的方式,然后在布局文件使用你要用的自定义控件,需要包含包名,再就是在activity中写控件的控制代码。

⑤ android 自定义控件 自定义控件 区别

你在layout布局的时候使用的什么Button啊,TextView啊之类的就是系统控件,自己定义的控件的话是通过继承View类来实现的。

publicclassMyViewextendsView{
//此处省略构造方法

privatevoidonDraw(Canvascanvas){
//重写view的onDraw方法,绘制控件的样式
//这里你使用canvas来绘制,你布局中使用这个控件就是你绘制的样子
}

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

大概就是这样实现的自定义控件,自定义控件的话优化是很重要的哦,不然性能会很差。
然后你要使用这个控件的话,在布局中就需要这样定义,假如这个自定义控件类是这样的:
xxx.xxx.MyView。
则使用时:
<xxx.xxx.MyView
这些地方一样的设置宽高,id啊杂七杂八的属性

/>

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

public
class
MyView
extends
View{
//
此处省略
构造方法
private
void
onDraw(Canvas
canvas){
//重写view的onDraw方法,绘制控件的样式
//这里你使用canvas来绘制,你布局中使用这个控件就是你绘制的样子
}
//然后你可以定义很多自己的一些方法,用来修改控件的样式
//假如你自定义的一个
进度条
的话,就要修改进度条值,你就可以自定义方法,让实现对象来改变进度值,记得修改后调用validate方法更新显示。(具体函数记不太清了)
}
大概就是这样实现的自定义控件,自定义控件的话优化是很重要的哦,不然性能会很差。
然后你要使用这个控件的话,在布局中就需要这样定义,假如这个自定义控件类是这样的:
xxx.xxx.MyView。
则使用时:

⑦ android自定义View组合控件

需要看你实现什么效果。一般自定义控件可以用2种方式:
1.
将现有的控件封装起来,作为一个通用的组件来使用,此时只能用代码的形式调用,无法再xml中引用,例如将textview和button封装为一个登录控件。
2.
继承view或者viewgroup,其实viewgroup也是继承的view。然后依次实现onmeasure()、onlayout()、ondraw(),
1.
onmeasure
--
负责测绘控件的大小
2.
onlayout
--
负责控件中子元素摆放的位置
3.
ondraw
--
负责控件和子控件的绘制,使其显示在屏幕中
4.
一些设计和实现较好的自定义view,一般还需要考虑事件的传递、动画的控制、touch事件的处理等

⑧ 电脑培训分享Android之自定义控件

一、简单自定义控件MyButton



每一个控件都是一个java类,有对应的代码,只要你能正确的编写java代码,那么电脑培训http://www.kmbdqn.com/发现可以创造出符合你需求的控件,即自定义控件。


1.通过继承的方式,创建自定义控件


通过继承一个现有的控件,覆盖其界面的呈现


通过继承一个包含若干子控件的布局


通过继承一个现有的控件,覆盖某个响应事件


继承一个View来完整自定义一个心控件


2.使你的自定义控件继承自某个最接近的Android控件,必须是public


一般都会调用父类的构造方法 ,注意一般有三个构造方法


覆盖原来控件的方法,注意是否要再调用super中的方法


在XML中以类全名的方式引用此控件


二、复杂自定义控件MyLogin


需要设计包含一组控件的自定义控件就需要用到复杂的自定义控件


1) 使得你的自定义控件继承自某个接近的布局


2) 正确的实现构造方法:构造方法中实例化目标布局,同时查找到各个子布局


3) 添加相应的响应代码来修改属性,使得外部能访问布局中的子控件


4) 在XML中以类全名的方式引用此控件,完整的包名+类名。


⑨ Android之自定义控件

一、简单自定义控件MyButton



每一个控件都是一个java类,有对应的代码,只要你能正确的编写java代码,那么电脑培训http://www.kmbdqn.cn/发现可以创造出符合你需求的控件,即自定义控件。


1.通过继承的方式,创建自定义控件


通过继承一个现有的控件,覆盖其界面的呈现


通过继承一个包含若干子控件的布局


通过继承一个现有的控件,覆盖某个响应事件


继承一个View来完整自定义一个心控件


2.使你的自定义控件继承自某个最接近的Android控件,必须是public


一般都会调用父类的构造方法,注意一般有三个构造方法


覆盖原来控件的方法,注意是否要再调用super中的方法


在XML中以类全名的方式引用此控件


二、复杂自定义控件MyLogin


需要设计包含一组控件的自定义控件就需要用到复杂的自定义控件


1)使得你的自定义控件继承自某个接近的布局


2)正确的实现构造方法:构造方法中实例化目标布局,同时查找到各个子布局


3)添加相应的响应代码来修改属性,使得外部能访问布局中的子控件


4)在XML中以类全名的方式引用此控件,完整的包名+类名。


热点内容
传智播客php韩顺平 发布:2024-05-17 20:09:52 浏览:819
android蓝牙打印打印图片 发布:2024-05-17 18:58:31 浏览:464
android年龄 发布:2024-05-17 18:51:33 浏览:198
termux安装python 发布:2024-05-17 18:44:55 浏览:655
手机流量上传 发布:2024-05-17 18:44:06 浏览:551
服务器怎么证明是好的 发布:2024-05-17 18:39:28 浏览:683
树莓派如何搭建mqtt服务器 发布:2024-05-17 18:27:38 浏览:437
门口机sip服务器ip是什么 发布:2024-05-17 17:38:27 浏览:554
光遇安卓区是什么服 发布:2024-05-17 17:22:25 浏览:25
linux驱动开发教程 发布:2024-05-17 17:19:52 浏览:501