AndroidAnnotations——Enhance custom views 优化自定义组件

本文介绍如何使用@EView和@EViewGroup注解创建自定义组件,简化代码重复并提高复用性。通过实例展示了自定义视图和视图组的具体实现及应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Enhance custom views 优化自定义组件


@EView and  @EViewGroup are the annotations to use if you want to create  custom components.
假如你想创建 自定义组件 ,你可以使用 @EView @EViewGroup 注解。

Why should I use custom components ?为什么我需要使用自定义组件?

If you notice that you're duplicating some parts of your layouts in different locations in your app, and that you're duplicating and calling the same methods again and again to control these parts, then a custom component can probably make your life a lot easier !如果你注意到你在自己app的不同位置复用布局中的一些部分,并且你也复用一些方法,一遍又一遍地调用它们来控制这些部分,那么一个自定义组件可能使你的生活更简单。


Custom Views with @EView  使用@EView的自定义视图

Since AndroidAnnotations 2.4

Just create a new class that extends View, and annotate it with @EView. You can than start using annotations in this view:只要创建一个继承View的新类,并用 @EView注解。你就可以在这个View中开始使用注解功能了:

@EView
public class CustomButton extends Button {

        @App
        MyApplication application;

        @StringRes
        String someStringResource;

        public CustomButton(Context context, AttributeSet attrs) {
                super(context, attrs);
        }
}

You can then start using it in your layouts (don't forget the _):然后你可以在布局文件中开始使用这个视图(别忘记 _):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.androidannotations.view.CustomButton_
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- ... -->

</LinearLayout>

You can also create it programmatically:你也可以用代码生成:

CustomButton button = CustomButton_.build(context);

Custom ViewGroups with@EViewGroup使用@EViewGroup的视图组

Since AndroidAnnotations 2.2

How to create it ?如何创建它?

First of all, let's create a layout XML file for this component.首先,让我们为这个组件创建一个布局xml文件。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <ImageView
        android:id="@+id/image"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/check" />

    <TextView
        android:id="@+id/title"
        android:layout_toLeftOf="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="12pt" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:textColor="#FFdedede"
        android:textSize="10pt" />

</merge>

Did you know about the merge tag ? When this layout will get inflated, the childrens will be added directly to the parent, you'll save a level in the view hierarchy.你知道merge标签吗?当布局将要被inflate的时候,子视图将被直接加到父容器中,你将保存一个view层级的等级。

As you can see I used some RelativeLayout specific layout attributes (layout_alignParentRight,layout_alignBottomlayout_toLeftOf, etc...), it's because I'm assuming my layout will be inflated in aRelativeLayout. And that's indeed what I'll do !如你所见,我使用了一些相对布局中的特殊布局attributeslayout_alignParentRightlayout_alignBottom, layout_toLeftOf等等),这是因为我假设我的布局会在一个相对布局中被inflate。并且我的确会这么做!

@EViewGroup(R.layout.title_with_subtitle)
public class TitleWithSubtitle extends RelativeLayout {

        @ViewById
        protected TextView title, subtitle;

        public TitleWithSubtitle(Context context, AttributeSet attrs) {
                super(context, attrs);
        }

        public void setTexts(String titleText, String subTitleText) {
                title.setText(titleText);
                subtitle.setText(subTitleText);
        }

}

There you are ! Easy, isn't it ?成了!很简单,不是吗?

Now let's see how to use this brand new component.现在让我们看看怎么使用这个新组件。

How to use it ? 如何使用?

A custom component can be declared and placed in a layout just as any other View:一个自定义组件就和其他视图一样,可以被声明、放置在布局中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <com.androidannotations.viewgroup.TitleWithSubtitle_
        android:id="@+id/firstTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <com.androidannotations.viewgroup.TitleWithSubtitle_
        android:id="@+id/secondTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <com.androidannotations.viewgroup.TitleWithSubtitle_
        android:id="@+id/thirdTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

As always, don't forget the _ at the end of the component name!一如既往地,请不要忘记在组建名后加上 _。

And because I'm using AA, I can just as easily get my custom components injected in my activity and use them!因为我在使用AA,所以我可以更方便地将我的自定义组件注入到我的activity中使用。

@EActivity(R.layout.main)
public class Main extends Activity {

        @ViewById
        protected TitleWithSubtitle firstTitle, secondTitle, thirdTitle;

        @AfterViews
        protected void init() {
                
                firstTitle.setTexts("decouple your code",
                                "Hide the component logic from the code using it.");
                
                secondTitle.setTexts("write once, reuse anywhere",
                                "Declare you component in multiple " +
                                "places, just as easily as you " +
                                "would put a single basic View.");
                
                thirdTitle.setTexts("Let's get stated!",
                                "Let's see how AndroidAnnotations can make it easier!");
        }

}

Most AA annotations are available in an @EViewGroupgive it a try !大部分AA注解适用于@EViewGroup尝试一下吧!

本文档的简单示例下载

内容概要:本文档主要展示了C语言中关于字符串处理、指针操作以及动态内存分配的相关代码示例。首先介绍了如何实现键值对(“key=value”)字符串的解析,包括去除多余空格和根据键获取对应值的功能,并提供了相应的测试用例。接着演示了从给定字符串中分离出奇偶位置字符的方法,并将结果分别存储到两个不同的缓冲区中。此外,还探讨了常量(const)修饰符在变量和指针中的应用规则,解释了不同类型指针的区别及其使用场景。最后,详细讲解了如何动态分配二维字符数组,并实现了对这类数组的排序与释放操作。 适合人群:具有C语言基础的程序员或计算机科学相关专业的学生,尤其是那些希望深入理解字符串处理、指针操作以及动态内存管理机制的学习者。 使用场景及目标:①掌握如何高效地解析键值对字符串并去除其中的空白字符;②学会编写能够正确处理奇偶索引字符的函数;③理解const修饰符的作用范围及其对程序逻辑的影响;④熟悉动态分配二维字符数组的技术,并能对其进行有效的排序和清理。 阅读建议:由于本资源涉及较多底层概念和技术细节,建议读者先复习C语言基础知识,特别是指针和内存管理部分。在学习过程中,可以尝试动手编写类似的代码片段,以便更好地理解和掌握文中所介绍的各种技巧。同时,注意观察代码注释,它们对于理解复杂逻辑非常有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值