`
1140566087
  • 浏览: 547689 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
博客专栏
2c4ae07c-10c2-3bb0-a106-d91fe0a10f37
c/c++ 入门笔记
浏览量:18076
3161ba8d-c410-3ef9-871c-3e48524c5263
Android 学习笔记
浏览量:309479
Group-logo
J2ME 基础学习课程集
浏览量:17993
A98a97d4-eb03-3faf-af96-c7c28f709feb
Spring 学习过程记录...
浏览量:17195
社区版块
存档分类
最新评论

Android 之 自定义控件用法介绍

阅读更多
自定义效果:实现:图片和文字混合

首先创建需要组合的子布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/myimage"
        android:layout_width="30dp"
        android:layout_height="30dp" />

    <TextView
        android:id="@+id/mytext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定" />

</LinearLayout>


编写代码,为自定义控件设置值:
package com.example.userdefinedview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 自定义 组建的实现 --
 * 功能:复合组件
 * @author Administrator
 *
 */
public class MyLinearLayout extends LinearLayout {

	// 声明对象属性
	private ImageView myimage;
	private TextView mytext; 
	
	public MyLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.my_linearlayout,this,true); //视图容器装载
		myimage = (ImageView) findViewById(R.id.myimage); // 获取对象
		mytext = (TextView) findViewById(R.id.mytext);
	}
	
	/**
	 * 设置图片资源
	 * @param resID 资源ID
	 */
	public void setImageViewImageResource(int resID){
		myimage.setImageResource(resID);
	}
	
	/**
	 * 设置文本内容
	 * @param text 字符信息
	 */
	public void setMyTextText(String text){
		mytext.setText(text);
	}
	
}



主布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <com.example.userdefinedview.MyLinearLayout
        android:id="@+id/my1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="#ff00ff"
         />

    <com.example.userdefinedview.MyLinearLayout
        android:layout_marginLeft="20dp"
        android:id="@+id/my2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
       	android:background="#ff00ff"
       />

</LinearLayout>




主程序入口代码:
package com.example.userdefinedview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	private MyLinearLayout my1;
	private MyLinearLayout my2;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		my1 = (MyLinearLayout) findViewById(R.id.my1);
		my2 = (MyLinearLayout) findViewById(R.id.my2);

		my1.setImageViewImageResource(R.drawable.a1);
		my1.setMyTextText("确定");

		my2.setImageViewImageResource(R.drawable.a6);
		my2.setMyTextText("取消");

		my1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				Toast.makeText(MainActivity.this, "点击了确定",Toast.LENGTH_LONG).show();
			}
		});
		my2.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "点击了取消",Toast.LENGTH_LONG).show();
			}
		});
	}



}
分享到:
评论

相关推荐

    Android自定义控件源码含APK 仿Material Design风格.rar

    Android自定义控件源码含APK 仿Material Design风格,在低版本android环境上面实现高版本中的按钮等UI控件,视觉效果提升,本源码将向你介绍一些方法,实现这种效果。注:本源码中使用的控件是原作者已经封装好的,...

    Android 自定义标题导航控件

    希望大家喜欢一起学习。 本文目录主要如下: 1.自定义控件属性的定义 2.自定义控件的java代码 3.自定义控件属性的用法 4.控件项目的打包处理 5.其他项目的使用

    省市区/县,详细地址选择自定义控件

    使用方法: android:id="@+id/test_edit" android:layout_width="match_parent" android:layout_height="wrap_content"/&gt; listEditTextView=(ListEditTextView)findViewById(R.id.test_edit); listEditTextView....

    实例讲解Android自定义控件

    Android开发之自定义控件用法详解 详解Android自定义控件属性 可以看到QQ上的ToolBar其实就是一个自定义的view,可以看到不同的界面就是简单地修改了文字而已,在第二张与第三张尤其的明显,我们就仿QQ的这个...

    Android 自定义时间日期控件

    Android 自定义时间日期控件 使用方法参考:http://blog.csdn.net/susanyuanaijia/article/details/53508295

    Android自定义日历控件实例详解

    下面我通过我在github上开源的Android-CalendarView项目为例,来介绍一下自定义控件的方法。该项目中自定义的控件类名是CalendarView。这个自定义控件覆盖了一些自定义控件时常需要重写的一些方法。 构造函数 为了...

    自定义控件练习Demo4

    原博客地址:https://blog.csdn.net/u010356768/article/details/89462624 练习自定义控件、自定义属性的基本用法

    自定义控件和手机安全助手

    custom工程演示了App开发的自定义控件相关知识,包括:自定义视图的步骤(声明属性、构造对象、测量尺寸、绘制视图)、自定义简单动画(任务片段、下拉刷新动画、圆弧进度动画)、自定义对话框的操作(对话框、改进...

    Android开发之自定义控件用法详解

    本文实例讲述了Android开发之自定义控件用法。分享给大家供大家参考,具体如下: 今天和大家分享下组合控件的使用。很多时候android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承...

    android自定义弹幕控件

    android自定义弹幕控件 一打包 相关的使用方法及实现都在博客 http://blog.csdn.net/u012815217/article/details/49745375中提到

    Android自定义控件EditText使用详解

    主要为大家详细介绍了Android自定义控件EditText的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    Android编程实现自定义控件的方法示例

    主要介绍了Android编程实现自定义控件的方法,结合实例形式分析了Android自定义控件的布局、功能实现与使用技巧,需要的朋友可以参考下

    Android控件架构与自定义控件详解(二)——自定义View

    在自定义View时,我们通常会去重写onDraw()方法来绘制View的显示内容。如果该View还需要使用wrap_content属性,那么还必须重写onMeasure()方法。另外,通过自定义attrs属性,还可以设置新的属性配置值。

    Android自定义控件之日期选择控件使用详解

    主要为大家详细介绍了Android自定义控件之日期选择控件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    Android 自定义菜单

    Android2.2版本中,如果你要修改菜单默认的背景颜色,网络上有许多的方法。在此就不说了。但是,如果要在2.2以上的版本修改...因此自己写了一个自定义的菜单控件,可以自由定义菜单的样式,使得菜单的样式更加丰富。

    可支持快速搜索筛选的Android自定义选择控件

    Android 自定义支持快速搜索筛选的选择控件使用方法,具体如下 项目中遇到选择控件选项过多,需要快速查找匹配的情况。 做了简单的Demo,效果图如下: 源码地址:https://github.com/whieenz/SearchSelect 这个...

    自定义Android可滑动控件源码

    实现了一个可滑动显示图片的控件,用于了解Android的触控机制,Scroller和VelocityTracker的用法

    android 常用控件布局汇总

    android 常用控件如:ProgressBar、ActionBar、Switch、ListView 、PopupWindow....等的用法,各种Style的写法以及自定义常用控件 android 五大布局的汇总及用法

Global site tag (gtag.js) - Google Analytics