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

android自定义控件xml

发布时间: 2022-09-08 18:11:48

A. 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;
}
}
}

B. android开发 自定义控件怎么在xml文件中配置 尤其是最上面的<LinearLayout >那部分

自已控件就是你先写好你的控件代码,假如你自定义了一个MyButton,然后这个MyButton类你放在com.app包里面,那么你在xml配置代码如下

<com.app.MyButton
android:layout_heigjt........... 属性/>

希望能帮到您

C. android 自定义view 怎么获取xml属性

你说的是自定义view ,也就是你自己写了个自定义的UI控件,在xml中你只要将该控件写出来即可(如Button,TexView当作控件来写),例如:<view(你自定义的view的类名)/>,接下来你就懂了,在里面声明你要的属性

D. Android自定义控件 怎样设置内边距

自定义控件,实质是重写某个控件的方法和属性,同时也继承父控件的方法和属性,比如内边距,设置自定义控件内边距的方法: 在xml布局文件中使用标签: android:padding="", android:paddingLeft="", android:paddingTop="", android:bottom="" 如果想要设置外边距,使用下面标签: android:layout_margin="",android:layout_marginLeft,android:layout_marginRight,android:layout_marginTop,android:layout_marginBottom

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

一、简单自定义控件MyButton



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


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


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


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


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


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


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


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


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


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


二、复杂自定义控件MyLogin


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


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


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


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


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


F. Android之自定义控件

一、简单自定义控件MyButton



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


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


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


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


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


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


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


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


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


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


二、复杂自定义控件MyLogin


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


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


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


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


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


G. 我在Android 程序的xml文件中加载的自定义ImageView控件中如何访问Activity中的其他控件的句柄

用着个试试:
mTextView01 = (TextView)TalkRoundSpinView.this.findViewById(R.id.textView01);
自己想办法传进来一个Activity或Context
View view = View.inflate(context或Activity, R.layout.***, null);再
mTextView01 = (TextView)view.findViewById(R.id.textView01);

H. Android 自定义控件 动态设置高度

Android动态改变View控件大小的方法:
1、声明控件参数获取对象 LayoutParams lp;
2、获取控件参数: lp = 控件id.getLayoutParams();
3、设置控件参数:如高度。 lp.height -= 10;
4:、使设置生效:控件id.setLayoutParams(lp);
例如如要把Imageview下移200px: ImageView.setPadding( ImageView.getPaddingLeft(), ImageView.getPaddingTop()+200, ImageView.getPaddingRight(), ImageView.getPaddingBottom());

I. android中xml中有些控件的属性里面有 "app:.." ,此处的app:是什么意思和一般的android:有什么区别

区别是:这两个是声明的不同的命名空间,android的是系统的,app是自定义的。


Android自定义控件的属性,在xml中使用自己自定义的attr的时候,其中有一步就是要自定义一个xml的命名空间后然后再给自定义属性赋值,现在发现不知道什么时候开始Android把这个改了,现在发现可以统一用
xmlns:app="http://schemas.android.com/apk/res-auto"
而不是原来的:
xmlns:app="http://schemas.android.com/apk/App的Package名"
还有人提到在作为lib被使用的时候,也应该用res-auto

所以说区别就是如果你http://schemas.android.com/apk/后面写的是res/包名,那就是相关包名下的自定义属性,而res-auto就是所有的自定义包名。

J. 如何使用自定义的XML属性

一. 什么是自定义XML属性
在我们使用自定义的控件时,很多时候都需要定义一些不同于一般的XML属性前缀(如android:layout_width)的属性,比如这样 app:textColor,这些就是自定义控件需要用到的自定义控件属性。
二. 自定义XML属性有什么用
自定义XML属性的作用在于,在采取自定义的控件时,很多时候,系统的一般XML属性已经不能满足需求,比如我们在做一个具有描边效果的TextView时,就需要有额外定义的TextView外边框颜色和TextView内部颜色两种颜色。这时候,使用自定义XML属性,用户就可以很方便地在XML中配置额外的属性。
三. 怎么使用自定义XML属性
1.定义对应的属性

在values文件夹下新建一个attar_custom.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 自定义控件的名称 -->
<declare-styleable name="StrokeTextView">
<!-- 自定义的属性名称 和对应的单位 -->
<attr name="outerColor" format="color|reference" />
<attr name="innnerColor" format="color|reference" />
</declare-styleable>
</resources>

2.在XML中定义自定义属性
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<span style="color:#ff0000;">xmlns:app="http://schemas.android.com/apk/res-auto"</span>
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<<span style="color:#ff0000;">com.example.demo.StrokeTextView</span>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="28sp"
<span style="color:#ff0000;">app:outerColor="#000000"
app:innnerColor="#ffffff"</span>
android:layout_centerInParent="true"/>

</RelativeLayout>

注意,自定义的XML属性必须给自定义的控件使用。

热点内容
linux驱动开发教程 发布:2024-05-17 17:19:52 浏览:500
抖音中秋节视频脚本 发布:2024-05-17 17:19:51 浏览:193
快递柜为什么用安卓系统 发布:2024-05-17 17:17:18 浏览:906
电脑配置光纤接口怎么标注 发布:2024-05-17 17:06:56 浏览:976
如何用方向键控制安卓机 发布:2024-05-17 16:38:11 浏览:198
雨田系统源码 发布:2024-05-17 16:28:06 浏览:586
新手直播脚本 发布:2024-05-17 16:27:25 浏览:847
python双引号单引号 发布:2024-05-17 16:19:31 浏览:948
0xxc语言 发布:2024-05-17 16:17:40 浏览:700
php与java的区别 发布:2024-05-17 16:12:48 浏览:340