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

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尝试一下吧!

本文档的简单示例下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值