`
闷骚的小老头
  • 浏览: 26582 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

自定义控件之组合控件

阅读更多
在开发中很多用到自定义控件,其中大部分是有几个控件组合到一起使用,这里就联系到组合控件,我对自定义控件也不是太熟悉,昨天看写资料,自己写了个小demo,先从简单的来,组合控件,复杂的学会了在分享给大家,话不多说了,上代码吧
我们先举个例子,比如我们需要一个控件,左边一个文本,右边一个文本,下面一个横线,


下面附上demo地址
https://github.com/wudongze/WidgetDemo
来上代码
首先要把那块的布局实现出来

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="45dp">


    <TextView
        android:id="@+id/tv_left"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:padding="5dp" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:padding="5dp" />

    <TextView
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

然后定义自己需要的属性在attrs.xml文件中

<declare-styleable name="MyTextLayout">
    <attr name="left_text" format="string"/><!--左文本-->
    <attr name="right_text" format="string"/><!--右文本-->
    <attr name="line_color" format="color"/><!--线的颜色-->
    <attr name="left_text_size" format="dimension"/><!--左边文字的大小-->
    <attr name="right_text_size" format="dimension"/><!--右边文字的大小-->
    <attr name="left_text_color" format="color"/><!--左边文字的颜色-->
    <attr name="right_text_color" format="color"/><!--右边文字的颜色-->
    <attr name="is_show_line" format="boolean"/><!--是否显示横线-->
</declare-styleable>

这里name是属性名字 ,format是属性的类型,具体有什么类型会提示出来,根据自己的业务区选择
下面开始写代码了

public class MyTextLayout extends RelativeLayout {
    private TextView tvLeft, tvRight;
    private TextView line;
    private RelativeLayout relativeLayout;
    //这个构造函数用于代码中设置,也就是动态使用控件
    public MyTextLayout(Context context) {
        super(context);
    }
   //这个构造函数用于在布局文件中设置控件的属性
    public MyTextLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获取view
        inflate(getContext(), R.layout.text_layout, this);
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvRight = (TextView) findViewById(R.id.tv_right);
        line = (TextView) findViewById(R.id.line);
        relativeLayout = (RelativeLayout) findViewById(R.id.root);
       //获取TypedArray,这里面有我们定义的各种属性,如下
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attrs, R.styleable.MyTextLayout);
        String leftText = obtainStyledAttributes.getString(R.styleable.MyTextLayout_left_text);
        String rightText = obtainStyledAttributes.getString(R.styleable.MyTextLayout_right_text);
        int lineColor = obtainStyledAttributes.getColor(R.styleable.MyTextLayout_line_color, Color.parseColor("#ffffff"));
        float leftTextSize = obtainStyledAttributes.getDimension(R.styleable.MyTextLayout_left_text_size,15.0f);
        float rightTextSize = obtainStyledAttributes.getDimension(R.styleable.MyTextLayout_right_text_size,15.0f);
        int leftTextColor = obtainStyledAttributes.getColor(R.styleable.MyTextLayout_left_text_color,Color.parseColor("#000000"));
        int rightTextColor = obtainStyledAttributes.getColor(R.styleable.MyTextLayout_right_text_color,Color.parseColor("#000000"));
        boolean isShowLine = obtainStyledAttributes.getBoolean(R.styleable.MyTextLayout_is_show_line,true);
       //为view中的控件赋值
        tvLeft.setText(leftText);
        tvRight.setText(rightText);

        tvLeft.setTextSize(leftTextSize);
        tvRight.setTextSize(rightTextSize);

        tvLeft.setTextColor(leftTextColor);
        tvRight.setTextColor(rightTextColor);

        line.setBackgroundColor(lineColor);

        if (isShowLine) line.setVisibility(isShowLine ? VISIBLE : GONE);
    }
   //这个构造函数用于设置默认样式的控件
    public MyTextLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

   //下面是自己定义的一些方法,根据业务来   1、设置左边文本  2、获取左边的文本内容   3、设置右边的文本
   //4、获取右边的文本   5、设置横线的显示和隐藏  6、设置点击监听
    public void setLeftText(String leftText) {
        tvLeft.setText(leftText);
    }

    public String getLeftText() {
        return tvLeft.getText().toString();
    }

    public void setRightText(String rightText) {
        tvRight.setText(rightText);
    }

    public String getRightText() {
        return tvRight.getText().toString();
    }

    public void isShowLine(boolean flag) {
        line.setVisibility(flag ? VISIBLE : GONE);
    }

    public void setOnClickLintener(OnClickListener onClickLintener) {
        relativeLayout.setOnClickListener(onClickLintener);
    }
}

在使用的时候和正常的控件一样

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.wudz.widgetdemo.MyTextLayout
        android:id="@+id/my_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:is_show_line="false"
        app:left_text="商品价格"
        app:line_color="#000000"
        app:right_text="20元"></com.example.wudz.widgetdemo.MyTextLayout>

</RelativeLayout>

在这里就能使用我们定义的属性了,一定要注意,在使用自定义控件一定要加上

xmlns:app="http://schemas.android.com/apk/res-auto"

这个是命名空间
在代码中设置

myTextLayout = (MyTextLayout) findViewById(R.id.my_text);
myTextLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this,"点击了控件",Toast.LENGTH_SHORT).show();
        myTextLayout.setLeftText("商品价格2");
        myTextLayout.setRightText("40元");
        myTextLayout.isShowLine(true);
    }
});
0
0
分享到:
评论

相关推荐

    Android自定义控件之组合控件学习笔记分享

    我们来讲一下自定义组合控件,相信大家也接触过自定义组合控件吧,话不多说,直接干(哈~哈~): 大家看到这个觉得这不是很简单的吗,这不就是写个布局文件就搞定嘛,没错,确实直接上布局就行,不过,我只是用这个...

    vb6.0用户控件自定义控件

    用户控件是一种复合控件,它将多个标准VB控件组合在一起,形成一个单一的、可重用的单元。创建用户控件的过程涉及到以下步骤: 1. **创建新组件**: 在VB6.0环境中,选择"工程"菜单,然后点击"添加用户控件"。这将...

    labview自定义控件.rar

    - 使用LabVIEW的"控件工具栏",可以添加基本的UI元素,如按钮、指示器、文本框等,通过组合这些元素来构建自定义控件的外观。 - 编写完成后,可以通过"控件属性"对话框将VI标记为控件模板,这样就可以在其他程序中...

    自定义控件之组合式控件,下拉选择框

    在安卓应用开发中,自定义控件是提升用户体验和界面个性化的重要手段。本文将深入探讨如何创建一个自定义的组合式控件,特别是下拉选择框。下拉选择框(Dropdown Spinner)通常用于提供多个选项让用户进行单选,它...

    C#自定义控件之-winform美化

    本文将深入探讨如何在WinForms中进行自定义控件的创建和窗体美化,以解决系统默认控件外观不尽如人意的问题。 一、自定义控件的创建 1. 继承现有控件:你可以通过继承System.Windows.Forms中的控件类,如Button、...

    ASP.NET用户控件和自定义控件

    用户控件是ASP.NET中的基本自定义UI元素,它是通过将多个标准ASP.NET控件组合在一起,形成一个复合控件来实现的。用户控件可以看作是网页的微型版,具有自己的HTML、服务器控件和脚本。创建用户控件的主要步骤包括:...

    Android 自定义控件 组合控件

    在Android开发中,自定义控件是提升应用独特性和用户体验的重要手段。组合控件,顾名思义,是指将多个基本控件通过特定的方式组合在一起,形成一个具有新功能或新外观的复合控件。本篇文章将深入探讨如何在Android中...

    自定义控件(组合控件)源码(WebControl)

    自定义控件(组合控件)源码(WebControl) 自定义控件(组合控件)源码(WebControl) 自定义控件(组合控件)源码(WebControl)

    自定义控件属性与组合控件

    总结来说,自定义控件属性和组合控件是Android开发中的高级技巧,能够帮助开发者实现高度定制的用户界面。自定义属性允许我们为控件增加更多的可配置选项,而组合控件则让我们能够利用已有组件构建更复杂的视图。...

    【Android进阶】(1)用继承和组合方式自定义控件

    - **基础概念**:组合是指将多个现有的控件组合在一起,形成一个新的功能更强大的控件。这种方式更灵活,可以将复杂的功能分解为多个简单组件,便于维护和复用。 - **步骤**: 1. 在XML布局中嵌套多个控件,通过`...

    总结的winform自定义控件开发教程

    自定义控件是winform应用程序中最基本的组件之一,它们可以提供丰富的交互体验和个性化的界面设计。开发自定义控件需要具备一定的编程基础和winform开发经验。 winform自定义控件可以分为三种类型:复合控件...

    C#.net自定义控件开发用户自定义控件扩展控件

    其次,**用户控件(User Control)**是一种特殊类型的自定义控件,它是由多个现有的.NET控件组合而成的新控件。用户控件提供了一种可视化设计的方式,允许开发者在设计器中拖放控件并布局,就像构建一个小型的用户...

    labveiw自定义控件、以及一些各种布尔控件.zip

    标题“labview自定义控件、以及一些各种布尔控件.zip”表明这是一个包含多个自定义控件和布尔类型控件的压缩包资源。这些控件可能包括按钮、开关等交互元素,且特别强调了布尔类型的控件,这通常指的是能开启或关闭...

    安卓自定义控件相关-Android自定义控件源码.rar

    这个压缩包"Android自定义控件源码.rar"包含了一些自定义控件的源代码,虽然不能保证每个都可直接运行,但它们提供了丰富的学习资源,帮助开发者理解和实践自定义控件的创建过程。下面将详细探讨Android自定义控件的...

    WPF自定义控件(按钮文本框组合框等等)

    在Windows Presentation Foundation (WPF) 中,自定义控件是一种扩展和定制用户界面元素的方式,以便更好地满足应用程序的独特需求。WPF提供了丰富的内置控件,但有时这些控件可能无法完全满足我们的设计或功能需求...

    Android使用系统控件组合成新的自定义控件

    在Android开发中,自定义控件是提升应用用户体验和界面设计独特性的重要手段。通过将系统提供的基础控件进行组合和扩展,我们可以创造出满足特定需求的复杂组件。本教程将深入探讨如何在Android Studio中利用现有...

    C# 自定义控件 自定义ComboBox

    自定义控件是C#编程中一个重要的概念,它允许开发者根据需求扩展或修改内置控件的功能和外观,以满足特定项目的需求。在本案例中,我们将深入探讨如何自定义ComboBox控件。 ComboBox控件是Windows Forms中一个非常...

    minigui自定义控件2

    - 组件整合:如果你需要将多个标准控件组合成一个新的复杂控件,如日历选择器。 5. **注意事项** - 性能优化:自定义控件可能会增加程序的复杂性,因此要注意性能优化,避免过度绘制或不必要的消息处理。 - 兼容...

Global site tag (gtag.js) - Google Analytics