当前位置: 代码迷 >> 综合 >> AndroidAnnotations——Enhance custom views 优化自定义组件
  详细解决方案

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

热度:56   发布时间:2023-12-12 00:02:24.0
AndroidAnnotation

目录(?)[+]

  1. Enhance custom views 优化自定义组件
    1. Why should I use custom components 为什么我需要使用自定义组件
  2. Custom Views with EView  使用EView的自定义视图
  3. Custom ViewGroups withEViewGroup使用EViewGroup的视图组
    1. How to create it 如何创建它
    2. How to use it  如何使用
  4. 本文档的简单示例下载

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 {
        @AppMyApplication application;@StringResString 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" ><ImageViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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_alignBottom, layout_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 {
        @ViewByIdprotected 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 {
        @ViewByIdprotected TitleWithSubtitle firstTitle, secondTitle, thirdTitle;@AfterViewsprotected 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 @EViewGroup, give it a try !大部分AA注解适用于@EViewGroup尝试一下吧!

本文档的简单示例下载

  相关解决方案