简体   繁体   English

Android:自动为xml中的所有ID创建变量

[英]Android: automatically make variables for all IDs in xml

I noticed one of the most tedious parts of Android development is the layout design, even with layout builder.我注意到 Android 开发中最乏味的部分之一是布局设计,即使使用布局构建器也是如此。

After setting up the graphics, then the layout, making variable associations with the layout elements is very tedious, such as ImageButton myButton = (ImageButton)findViewById(R.id.myButton);设置好图形后,接下来是布局,与布局元素做变量关联非常繁琐,比如ImageButton myButton = (ImageButton)findViewById(R.id.myButton);

in larger layouts, these can get tedious to keep track of (recalling the names of the elements) and then the need to add more variables in any kind of order gets frustrating.在较大的布局中,跟踪(回忆元素的名称)可能会变得乏味,然后需要以任何顺序添加更多变量变得令人沮丧。

To slightly mitigate this, it would be very convenient if all of the IDs I declared in the XML were automatically associated with their proper variables, and all of those datatypes were already included in that class为了稍微缓解这种情况,如果我在 XML 中声明的所有 ID 都自动与其适当的变量相关联,并且所有这些数据类型都已包含在该类中,那将非常方便

Is there something that already does this?有什么东西已经这样做了吗?

for instance if I write例如,如果我写

 <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/myButton" android:id="@+id/myButton"></ImageButton>

then I would like the classes which include this layout to already have那么我希望包含此布局的类已经拥有

 import android.ImageButton;

 ImageButton myButton;

 myButton = (ImageButton)findViewById(R.id.myButton);

is this a setting or a feature to request?这是要请求的设置还是功能? I am using the Eclipse IDE and it would be very convenient我正在使用 Eclipse IDE,它会非常方便

I made a tool to automatically generate the Java code for tying layout XML's and program logic together.我制作了一个工具来自动生成用于将布局 XML 和程序逻辑联系在一起的 Java 代码。

Basically it takes an XML layout, and generates all the necessary Java code for you in an instant.基本上,它采用 XML 布局,并立即为您生成所有必需的 Java 代码。 There is support for basic member variables, ViewHolder pattern, ArrayAdapter, CursorAdapter and RoboGuice code types.支持基本成员变量、ViewHolder 模式、ArrayAdapter、CursorAdapter 和 RoboGuice 代码类型。

You can find it here: Android Layout Finder |你可以在这里找到它: Android Layout Finder | Buzzing Android 嗡嗡作响的安卓

Try using Android Annotations .尝试使用Android 注释 It provides useful annotations to replace boilerplate code.它提供了有用的注释来替换样板代码。

For instance, see @ViewById documentation : just declare the fields annotated例如,请参阅@ViewById 文档:只需声明带注释的字段

@ViewById
EditText myEditText;

@ViewById(R.id.myTextView)
TextView textView;

It replaces它取代

EditText myEditText;

TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
    [...]
    myEditText = (EditText) findViewById(R.id.myEditText);
    textView = (TextView) findViewById(R.id.myTextView);
}

Since version 3.6 of android studio they have officially added the support of autometically view binding that's generates a binding class for each XML layout file.从 android studio 3.6 版开始,他们正式添加了对自动视图绑定的支持,它为每个 XML 布局文件生成一个绑定类。 These classes contain direct references to all views that have an ID in the corresponding layout.这些类包含对在相应布局中具有 ID 的所有视图的直接引用。

you can enable it by modifying your build.gradle file.您可以通过修改build.gradle文件来启用它。

android {
  ...
  viewBinding {
      enabled = true
  }
}

If view binding is enabled for a module, a binding class is generated for each XML layout file that the module contains.如果为模块启用了视图绑定,则会为模块包含的每个 XML 布局文件生成一个绑定类。 Each binding class contains references to the root view and all views that have an ID.每个绑定类都包含对根视图和所有具有 ID 的视图的引用。

For example, given a layout file called result_profile.xml The generated binding class will be called ResultProfileBinding .例如,给定一个名为result_profile.xml的布局文件,生成的绑定类将被称为ResultProfileBinding

Example:例子:

private ResultProfileBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ResultProfileBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);
}

Please check the official documentation here .请在此处查看官方文档。

UPDATE : Android View Binding (AVB) Code Generator , this does exactly what i am going to say :|更新Android 视图绑定 (AVB) 代码生成器,这正是我要说的:|


REGEX! 正则表达式!

I had the same problem and i tried to solve it myself so i turned into REGEX inside of search and replace of dear Android Studio .我遇到了同样的问题,我试图自己解决它,所以我在搜索和替换亲爱的Android Studio变成了 REGEX。 Personally i use ButterKnife for dependency injection with java annotation but the more important part is how to automate the procedure of turning ids in xml layout to java objects.我个人使用ButterKnife进行带有 java 注释的依赖注入,但更重要的部分是如何自动化将 xml 布局中的 id 转换为 java 对象的过程。 Its kind of like the Android Layout Finder |它有点像Android Layout Finder | Buzzing Android (the website has more features but old :( ) answer but with annotation result. 嗡嗡作响的Android (该网站具有更多功能但很旧:()答案但带有注释结果。

  1. go to your xml and select all of the ids with this regex ( \\+id/.* ).转到您的xml并使用此正则表达式 ( \\+id/.* ) 选择所有\\+id/.* To do that first press Ctrl + F to open the search panel, then make sure that the Regex checkbox is checked.为此,请先按Ctrl + F打开搜索面板,然后确保选中Regex复选框。 then enter the regex ( \\+id/.* ) inside the box and then press Ctrl + Alt + Shift + J to select all occurrences.然后在框中输入正则表达式 ( \\+id/.* ),然后按Ctrl + Alt + Shift + J选择所有匹配项。 Now Ctrl + C to copy them (u know the shortcut).现在Ctrl + C复制它们(你知道快捷方式)。

for example i had this layout:例如我有这个布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="rtl"
    tools:context=".jobs.return_from_entrance.ReturnFromEntranceActivity"
    tools:ignore="HardcodedText">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridLayout
            android:id="@+id/return_entrance_grid_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:columnCount="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="شماره برگشت" />

            <TextView
                android:id="@+id/return_entrance_return_entrance_number_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="123123123"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="@string/owner_of_cargo" />

            <TextView
                android:id="@+id/return_entrance_owner_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="الجی"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="تاریخ و زمان" />

            <TextView
                android:id="@+id/return_entrance_time_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="12/12/12/ 12:12"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="نوع حواله" />

            <TextView
                android:id="@+id/return_entrance_kind_of_order_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="حواله"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="خریدار" />


            <TextView
                android:id="@+id/return_entrance_buyer_name_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="علی امیدی"
                android:textColor="@android:color/black"
                android:textSize="18sp" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="مقصد" />

            <TextView
                android:id="@+id/return_entrance_destination_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="آزادی"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="وزن ناخالص" />

            <TextView
                android:id="@+id/return_entrance_gross_weight_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="123"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="شماره جواز" />

            <TextView
                android:id="@+id/return_entrance_permission_number_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="126545643"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="شماره بارنامه" />

            <TextView
                android:id="@+id/return_entrance_waybill_number_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="654"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="زمان ورود" />

            <TextView
                android:id="@+id/return_entrance_enter_time_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="21/12/12 22:22"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="زمان خروج" />

            <TextView
                android:id="@+id/return_entrance_exit_time_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="21/12/12 22:22"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="محوطه بارگیری" />

            <TextView
                android:id="@+id/return_entrance_load_location_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="حیاط"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="نیاز به جرثقیل" />

            <TextView
                android:id="@+id/return_entrance_is_crane_needed_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="ندارد"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="نیاز به لیفتراک" />

            <TextView
                android:id="@+id/return_entrance_is_forklift_needed_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="ندارد"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <CheckBox
                android:id="@+id/return_entrance_internal_return_entrance_checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/margin_large"
                android:layout_marginStart="@dimen/margin_medium"
                android:layout_marginTop="@dimen/margin_large"
                android:text="خروج داخلی" />

            <View
                android:layout_width="0dp"
                android:layout_height="0dp" />

            <CheckBox
                android:id="@+id/return_entrance_warehouse_delivery_checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/margin_large"
                android:layout_marginStart="@dimen/margin_medium"
                android:layout_marginTop="@dimen/margin_large"
                android:text="تحویل در انبار" />

            <View
                android:layout_width="0dp"
                android:layout_height="0dp" />

            <Button
                android:id="@+id/return_entrance_location_delivery_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_large"
                android:text="تحویل در محل" />

            <View
                android:layout_width="0dp"
                android:layout_height="0dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="عکس راننده" />

            <ImageView
                android:id="@+id/return_entrance_driver_image_view"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/item_margin"
                android:src="@drawable/ic_account_circle_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="@string/name_of_driver" />

            <TextView
                android:id="@+id/return_entrance_name_of_driver_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="علی امیدی"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="@string/kind_of_car" />

            <TextView
                android:id="@+id/return_entrance_kind_of_car_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="وانت مزدا"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="@string/plaque" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_large"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/return_entrance_plaque_2digit_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/plaque_background"
                    android:padding="10dp"
                    android:text="11"
                    android:textColor="@android:color/black"
                    android:textSize="10pt"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/return_entrance_plaque_6digit_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/plaque_background"
                    android:padding="10dp"
                    android:text="999ج77"
                    android:textColor="@android:color/black"
                    android:textSize="10pt"
                    android:textStyle="bold" />
            </LinearLayout>
        </GridLayout>

        <Button
            android:id="@+id/return_entrance_barcode_scan_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="@dimen/margin_small"
            android:drawableStart="@drawable/ic_barcode"
            android:padding="@dimen/margin_medium"
            android:text="@string/scan_barcode"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/return_entrance_grid_layout" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/return_entrance_cargo_list_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/return_entrance_barcode_scan_button" />

        <GridLayout
            android:id="@+id/return_entrance_bottom_grid_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:columnCount="2"
            android:layoutDirection="rtl"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/return_entrance_cargo_list_recycler_view">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="میزان موجودی کالای قابل تحویل" />

            <TextView
                android:id="@+id/return_entrance_deliverable_availability_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="50"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

        </GridLayout>

        <LinearLayout
            android:id="@+id/return_entrance_bottom_linear_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/return_entrance_bottom_grid_layout">

            <Button
                android:id="@+id/return_entrance_cost_report_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_medium"
                android:text="گزارش هزینه" />

            <Button
                android:id="@+id/return_entrance_confirm_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_medium"
                android:text="تایید برگشت" />

        </LinearLayout>

    </android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>

a lot of text views to fill, huh?很多文本视图要填充,是吧?

  1. go to your java class and paste the copied ids( Ctrl + V ).转到您的Java类并粘贴复制的 ID( Ctrl + V )。 so we have bunch of ids which we want to change them into java objects.所以我们有一堆id,我们想把它们改成java对象。 in our example, my ids would be like this :在我们的示例中,我的 ID 将是这样的:

     +id/return_entrance_grid_layout" +id/return_entrance_return_entrance_number_text_view" +id/return_entrance_owner_text_view" +id/return_entrance_time_text_view" ...

Its time to find and replace!是时候寻找和更换了! so first we hit Ctrl + R to open the find and replace panel.所以首先我们Ctrl + R打开查找和替换面板。 (make sure the REGEX checkbox is checked) now im going to do some find and replaces to get the ideal result: (确保选中REGEX复选框)现在我要做一些查找和替换以获得理想的结果:

  1. Find : \\+id/(.*)" Replace with: @BindView(R.id.$1) so we will have this:查找: \\+id/(.*)"替换为: @BindView(R.id.$1)所以我们会有这个:

     @BindView(R.id.return_entrance_grid_layout) @BindView(R.id.return_entrance_return_entrance_number_text_view) @BindView(R.id.return_entrance_owner_text_view) @BindView(R.id.return_entrance_time_text_view) ...

now its time to define each variable type and name them.现在是定义每个变量类型并命名它们的时候了。 my xml naming has WHERE_DESCRIPTION_WHAT pattern, ( something like this ).我的 xml 命名有WHERE_DESCRIPTION_WHAT模式,(像这样)。 so for the variable name i want to remove the WHERE part.所以对于变量名,我想删除WHERE部分。 and then define the object type .然后定义对象类型 so here we go:所以我们开始:

  1. Find: (@BindView\\(R\\.id\\.return_entrance_(.*)_text_view\\)) Replace with: $1 TextView $2TextView;查找: (@BindView\\(R\\.id\\.return_entrance_(.*)_text_view\\))替换为: $1 TextView $2TextView; the result will be:结果将是:

     @BindView(R.id.return_entrance_grid_layout) @BindView(R.id.return_entrance_return_entrance_number_text_view) TextView return_entrance_numberTextView; @BindView(R.id.return_entrance_owner_text_view) TextView ownerTextView; @BindView(R.id.return_entrance_time_text_view) TextView timeTextView; @BindView(R.id.return_entrance_kind_of_order_text_view) TextView kind_of_orderTextView; ...

(just hit a Ctrl + Alt + L to reformat your code) the names are ulgy :( . so we do this to camelCase! : (只需Ctrl + Alt + L即可重新格式化您的代码)名称很丑 :( 。所以我们对驼峰大小写!:

  1. Find: TextView \\b(.*)_(.*) Replace With: TextView $1\\u$2\u003c/code> and the result will be:查找: TextView \\b(.*)_(.*)替换为: TextView $1\\u$2\u003c/code>结果将是:

     @BindView(R.id.return_entrance_owner_text_view) TextView ownerTextView; @BindView(R.id.return_entrance_time_text_view) TextView timeTextView; @BindView(R.id.return_entrance_kind_of_order_text_view) TextView kind_ofOrderTextView; @BindView(R.id.return_entrance_buyer_name_text_view) TextView buyerNameTextView; @BindView(R.id.return_entrance_destination_text_view) TextView destinationTextView;

if you repeat the last part, any name that has more than one underline, each _ will be replaced with the Uppercase of the next character.如果您重复最后一部分,任何有多个下划线的名称,每个_将被替换为下一个字符的大写。 so in this example if i do the Find: TextView \\b(.*)_(.*) Replace With: TextView $1\\u$2\u003c/code> again, my TextView kind_ofOrderTextView;所以在这个例子中,如果我执行 Find: TextView \\b(.*)_(.*) Replace With: TextView $1\\u$2\u003c/code>再次,我的TextView kind_ofOrderTextView; will be TextView kindOfOrderTextView;将是TextView kindOfOrderTextView;

It may seem a bit complicated but when u get used to it, it becomes so fast and it becomes so more useful!它可能看起来有点复杂,但是当你习惯了它时,它会变得如此之快,变得如此有用! for example imagine in an MVP , u have a Model with the same String names of the TextViews , so u can do this to set all of their texts' from the Model with similar approach...例如,想象在MVP ,您有一个ModelTextViews String名称相同,因此您可以这样做以使用类似的方法从Model设置所有文本...

That's a feature request.这是一个功能请求。 It's very good one and I think it would be pretty useful;这是非常好的一个,我认为它会非常有用; on the other hand there are cases where it wouldn't work very well, for example if you are inflating views dynamically and don't know, at compile time, which view a particular Activity will inflate.另一方面,在某些情况下它不会很好地工作,例如,如果您动态地膨胀视图并且在编译时不知道特定 Activity 将膨胀哪个视图。 However, I tend to think this case would be the exception rather than the rule.但是,我倾向于认为这种情况是例外而不是规则。

The easiest way to do that would be to code a script that scans your layout XML files in search for components with ID's and creates .java files with the proper definitions.最简单的方法是编写一个脚本来扫描您的布局 XML 文件以搜索具有 ID 的组件并创建具有正确定义的 .java 文件。 Then, your activities could derive from those auto-generated classes.然后,您的活动可以从这些自动生成的类派生。 Something like this:像这样的东西:

When processed by your script, that generates a class:当由您的脚本处理时,会生成一个类:

class FooBarLayoutActivityBase extends Activity ... {
  protected ImageButton myButton;

  FooBarLayoutActivityBase() {
    myButton = (ImageButton)findViewById(R.id.myButton);
  }
}

Then, you can simply inherit from that base class to use the components...然后,您可以简单地从该基类继承以使用组件...

The script approach is simple and doesn't require you to delve into the toolchain's code -- but you could also do it directly in the ADT plugin.脚本方法很简单,不需要您深入研究工具链的代码——但您也可以直接在 ADT 插件中进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM