Android如何使用自定义控件
来源:互联网 发布:正版linux 编辑:程序博客网 时间:2024/04/27 16:22
实现一个如图所示的自定义控件
实现自定义控件的步骤:
- 自定义属性(attr.xml)
- 自定义控件(Tobbar.java)
- 引用自定义控件activity_main.xml)
- 主程序调用(MainActivity.java)
1.自定义属性(attr.xml)
<?xml version="1.0" encoding="utf-8"?><resources> <!-- declare-styleable:自定义属性的值 --> <declare-styleable name="TopBar"> <attr name="title" format="string" /> <attr name="titleTextSize" format="dimension" /> <attr name="titleTextColor" format="color" /> <attr name="leftTextColor" format="color" /> <attr name="leftBackground" format="reference|color" /> <attr name="leftText" format="string" /> <attr name="rightTextColor" format="color" /> <attr name="rightBackground" format="reference|color" /> <attr name="rightText" format="string" /> </declare-styleable></resources>自定义属性详解
2.自定义控件(Tobbar.java)
package com.sky.customwidget;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.*;public class Topbar extends RelativeLayout {private Button leftButton, rightButton;private TextView tvTitle;private int leftTextColor;private Drawable leftBackground;private String leftText;private int rightTextColor;private Drawable rightBackground;private String rightText;private float titleTextSize;private int titleTextColor;private String title; private LayoutParams leftParams, rightParams, titleParams;public Topbar(Context context, AttributeSet attrs) {super(context, attrs);//使用TypedArray存储获取到的自定义属性,查看View的源代码3281行TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);leftText = ta.getString(R.styleable.Topbar_leftText);rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);rightText = ta.getString(R.styleable.Topbar_rightText);titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);title = ta.getString(R.styleable.Topbar_skytitle);ta.recycle();leftButton = new Button(context);rightButton = new Button(context);tvTitle = new TextView(context);leftButton.setTextColor(leftTextColor);leftButton.setBackgroundDrawable(leftBackground);leftButton.setText(leftText);rightButton.setTextColor(rightTextColor);rightButton.setBackgroundDrawable(rightBackground);rightButton.setText(rightText);tvTitle.setTextColor(titleTextColor);tvTitle.setTextSize(titleTextSize);tvTitle.setText(title);tvTitle.setGravity(Gravity.CENTER);setBackgroundColor(0xFFF59563);leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.FILL_PARENT);leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);addView(leftButton, leftParams);rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.FILL_PARENT);rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);addView(rightButton, rightParams);titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);titleParams.addRule(RelativeLayout.CENTER_IN_PARENT);addView(tvTitle, titleParams); }}
这里我们继承了RelativeLayout,调用了一部分RelativeLayout的属性,要想实现更加自由的布局,可以直接继承View类
3.引用自定义控件activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:sky="http://schemas.android.com/apk/res/com.sky.customwidget" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.sky.customwidget.Topbar android:id="@+id/topBar" android:layout_width="fill_parent" android:layout_height="40dp" sky:leftBackground="#323412"sky:leftText="Back"sky:leftTextColor="#FFFFFF"sky:rightBackground="#123f12"sky:rightText="More"sky:rightTextColor="#FFFFFF"sky:skytitle="Title"sky:titleTextColor="#123412"sky:titleTextSize="10sp"></com.sky.customwidget.Topbar></RelativeLayout>注意:要定义xmlns:sky="http://schemas.android.com/apk/res/包名",之后才能写sky:XXX
而且这里的XXX要与attr.xml里定义的名称一一对应
4.主程序调用(MainActivity.java)
package com.sky.customwidget;import android.os.Bundle;import android.app.Activity;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
这样,我们的自定义控件就完成了,可是如果我们想要将这个自定义控件封装成jar包,并实现左右Button的单击事件该怎么做呢?
自定义控件进阶(请听下回分解~~)
0 0
- Android如何使用自定义控件
- 如何使用自定义控件
- Android自定义控件:如何使用自定义的XML属性
- Android自定义控件:如何使用自定义的XML属性
- Android简单了解自定义控件和自定义属性如何使用
- Android中如何使用自定义view 自定义控件属性及动态自定义控件
- Android如何实现自定义控件
- android 如何打包自定义控件
- android 如何打包自定义控件
- C# 自定义控件如何使用
- Android使用View自定义控件
- android自定义控件属性使用
- Android 自定义控件的使用
- android 使用attrs自定义控件
- android使用AttributeSet自定义控件
- 使用Android原生控件自定义Switch控件
- Android使用后自定义控件如何在该布局文件以外动态的设置自定义控件的属性
- Android自定义控件系列三:如何画画
- 实现两个字符串的连接(strcat函数)
- 什么是好代码,为什么要写好代码,怎样写出好代码
- Java ClassLoader原理分析
- 黑马程序员——JDK5.0新特性
- button点击效果
- Android如何使用自定义控件
- hdu 1328 IBM Minus One(模拟)
- armv6、armv7、armv7s、arm64 与开发静态库(.a)
- java工程打包成jar
- [ios] tableView:cellForRowAtIndexPath: unrecognized selector sent to instance
- Ubuntu上的OGRE开发环境搭建
- Apple Watch人机交互指南:UI设计基础--为Apple Watch而设计
- iOS导航栏标题字体的颜色字体的大小
- Android事件分发从例子角度讲解