android自定义控件

自定义控件 TitleBar

实现步骤:

 

1.先写好一个公共的自定义控件布局文件 custom_widget.xml
2.在其它布局文件里引用自定义控件(PublicTitleBar) activity_custom.xml
3.写自定义控件的类,主要是提供一些方法 PublicTitleBar.java
4.在 activity 里面设置自定义控件 文字/点击事件等 MainActivity.java
 




先写好一个公共的自定义控件布局文件 custom_widget.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#FFF000">

    <!-- org.aking86.test.userdefinedwidget.MyWidget -->

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/title"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="@string/back" />

    <Button
        android:id="@+id/btnOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/ok" />

</RelativeLayout>

 

 

在其它布局文件里引用自定义控件(PublicTitleBar) activity_custom.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <org.aking86.test.userdefinedwidget.PublicTitleBar

        android:id="@+id/pub_titlebar"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/pub_titlebar_height" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/pub_titlebar"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="32dp" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:hint="@string/entry_the_title" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/btnSetTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/settitle" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/linearLayout2"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="200dp"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>
 

写自定义控件的类,主要是提供一些方法 PublicTitleBar.java

package org.aking86.test.userdefinedwidget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PublicTitleBar extends RelativeLayout {
	private View view;
	private Button btnBack, btnOK;
	private TextView tvTitle;
	Context context;

	public PublicTitleBar(Context context) {
		super(context);
		this.context = context;
		initLayoutInflater();
	}

	public PublicTitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initLayoutInflater();
	}

	public PublicTitleBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		initLayoutInflater();
	}

	private void initLayoutInflater() {
		LayoutInflater lInflater = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //
		view = lInflater.inflate(R.layout.custom_widget, this);

		initView();
	}

	private void initView() {
		btnBack = (Button) view.findViewById(R.id.btnBack);
		btnOK = (Button) view.findViewById(R.id.btnOK);
		tvTitle = (TextView) view.findViewById(R.id.tvTitle);
	}

	public void setTitle
(String text) {
		tvTitle.setText(text);
	}

	@Override


	public void setOnClickListener
(OnClickListener lsn) {
		btnBack.setOnClickListener(lsn);
		btnOK.setOnClickListener(lsn);
		tvTitle.setOnClickListener(lsn);
	}

}

 

在 activity 里面设置自定义控件 文字/点击事件等

	/**
	 * 设置 自定义控件
	 */
	private void setPublicTitleBar() {
		PublicTitleBar titleBar = (PublicTitleBar) findViewById(R.id.pub_titlebar);

		titleBar.setTitle
("Welcome....");

		titleBar.setOnClickListener
(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.btnBack:
					Toast.makeText(getBaseContext(), "btnBack",
							Toast.LENGTH_SHORT).show();
					break;
				case R.id.btnOK:
					Toast.makeText(getBaseContext(), "btnOK",
							Toast.LENGTH_SHORT).show();
					break;
				case R.id.tvTitle:
					Toast.makeText(getBaseContext(), "Title...",
							Toast.LENGTH_SHORT).show();
					break;
				default:
					Toast.makeText(getBaseContext(), "default...",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		});
	}
android 自定义控件TabView 自学安卓开发两个月有余,现在学到了自定义控件,故贴上代码纪念一下... 这个自定义控件为我叫它TabView,具体用法其实很简单。以下介绍了详细用法 import android.content.Context; import android.util.AttributeSet; import android.view.Gravity; import android.view.ViewGrou 阅读详情

相关推荐

Android:自定义控件

Android:自定义控件

偶是不器的博客 2203

Android自定义控件_安卓自定义控件

MeasureSpec是View的内部类,它封装了一个View的尺寸,在onMeasure()当中会根据这个MeasureSpec的值来确定View的宽高。自定义View在Android的开发中的重要性还是很大的,因为仅仅靠系统提供的控件和组件,无论是美观度还是使用度,再或者是新特性上,都无法满足特定的业务场景。layout()过程,对于View来说用来计算View的位置参数,对于ViewGroup来说,除了要测量自身位置,还需要测量子View的位置。这里我们介绍最复杂的一种,自定义View。

xiaoqi6的博客 953

android自定义控件:根据声音波动的声波控件

直接上图,有两个效果,如下: 效果一: 效果二: 大家可能觉得效果二难看,但其实 ,在项目中的运行以后,是这样的: 因为这个控件的许多东西,都是可以自定义配置的,所以背景色,线的颜色等,下面详细介绍,最后会放出源码:

carlos1992的专栏 2万+

Android UI 自定义控件大全

很多东西的实用性还是蛮高的,转载让更多人看到。 原文链接: https://github.com/opendigg/awesome-github-android-ui 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 ViewPager 图表(Chart) 菜单(M...

晨曦 4442

Android安卓自定义控件实现点击事件等逻辑

要实现Android自定义控件,以便能够进行复用,这里尽行了一些简单的点击事件的设置,还可以进行其他的设置,原理一样 首先需要在layout里面创建一个xml布局文件 xml version="1.0" encoding="utf-8"?> LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

qq_40015237的博客 1764

Android 自定义标题栏控件(一)+安卓开发必备网站

自定义标题栏控件 先安利一个安卓开发必备图标网站:阿里巴巴矢量图库 我们平时所用的控件都是继承自View的,所以有很多控件是我们不想要的,那应该怎样自定义控件呢?      我们利用引用布局的方式来使用自定义的布局: 先新建一个diy.xml: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android=...

编程书生的博客 782

安卓转圈loading_Android自定义控件loading等待

郑州app开发自定义控件loading等待。因为在制作项目中,需要自己动手制作空间。下面是关于loading等待控件的全部代码。import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;im...

weixin_39732491的博客 449

android toolbar教程,安卓自定义组合控件--toolbar,安卓控件--toolbar

安卓自定义组合控件--toolbar,安卓控件--toolbar最近在学习安卓APP的开发,用到了toolbar这个控件, 最开始使用时include layout这种方法,不过感觉封装性不好,就又改成了自定义组合控件的方式。使用的工具为android studio 2.2,简称AS吧1.首先创建一个新的自定义控件,如下图。AS会创建3个文件, 一个java文件,一个layout中的xml文件(...

weixin_34521351的博客 696

android自定义控件 button,自定义控件实现button功能,重写点击方法后安卓崩溃

public TitleLayout(Context context, AttributeSet attrs) {super(context, attrs);// 利用layouInflate.from(context).inflate(传入layout,父活动)LayoutInflater.from(context).inflate(R.layout.title, this);Button ti...

weixin_39613208的博客 212

安卓数字倒数控件_Android 自定义倒计时控件CountdownTextView

效果如下:剩余 00:59:21package com.bihu.advertiserapp.widgets;import android.annotation.TargetApi;import android.content.Context;import android.icu.text.SimpleDateFormat;import android.os.Handler;import andr...

weixin_39750410的博客 280

android控件动态设置style,安卓全局设置自定义Style继承原生控件

有时候需要更改TextView的默认颜色,最近安卓10上面如果TextView自己没设置默认的颜色的话它是默认白色的,很容易造成看不清的问题。更改的话一个一个改太麻烦,下面直接几行代码全部搞定:在应用的values/styles .xml中更改定义自定义应用全部button控件的style:代码:@drawable/selector_button_background@drawable/selec...

weixin_34020067的博客 1086

Android自定义控件_安卓自定义控件(1)

MeasureSpec是View的内部类,它封装了一个View的尺寸,在onMeasure()当中会根据这个MeasureSpec的值来确定View的宽高。layout()过程,对于View来说用来计算View的位置参数,对于ViewGroup来说,除了要测量自身位置,还需要测量子View的位置。整个测量过程的入口位于View的measure方法当中,该方法做了一些参数的初始化之后调用了onMeasure方法,这里我们主要分析onMeasure。为了方便配置自定义View的属性,我们也可以自定义属性值。

xiaoqi6的博客 813

Android 自定义控件

Android 自定义控件是指开发者根据项目需求,通过继承现有的控件或直接继承 View 类,定制开发的控件。与系统提供的标准控件相比,自定义控件通常具有更加个性化的样式、功能和行为,能够更好地满足特定的 UI 设计要求和用户体验需求。自定义控件可以应用于各种需求,例如:自定义按钮、图表、动画效果、复杂的布局、响应特定交互等。其核心目的是增强应用的灵活性和可定制性。这种方式通过继承 Android 系统提供的标准控件,重写其方法来实现自定义的功能或样式。

L1442760086的博客 1794

Android自定义Switch控件

修改后的MySwitch控件接口基本与原Switch控件一致,并且除了可支持所有SDK外,增加了2项小功能: 1. 支持用Track背景图片的方式代替Texton Textoff等文字方式表现开关状态 2.支持调整控制Switch的高度

xuwei527的专栏 2万+

安卓学习笔记——自定义控件

目录〇、不使用自定义控件一、扩展控件二、重新绘制控件三、基于控件容器的自定义控件 在很多安卓岗位的职位描述上,都会提到一个“自定义控件”。这个东西上手其实并不难,但真想做好自定义控件,要会的东西还挺多。下面我就分享个简单的例子,给自己,也给需要的人。 需求:服务端传过来的数据长这个样子: 这是一个<important>甲方爸爸</important>特别<import...

u012416064的博客 214

Android自定义控件

导语 当系统控件不能满足我们的需求的时候,这时候我们就需要自定义控件,根据我们的需求来定制一个能满足我们需求的控件。一个让用户熟悉的控件才是一个好的控件,如果一味追求酷炫的效果,会让用户觉得华而不实。 主要内容 了解自定义控件 对现有控件进行拓展 创建复合控件 重写View来实现全新控件 自定义ViewGroup 具体内容自定义控件可以是对现有控件进行拓展、创建复合控件、重写View实现全新控件

Super丶C 483
上一篇: ratingBar demo
mrAking
博客等级 码龄16年 2粉丝 30原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值