R.id访问每个组件是正常的Android Studio [英] Android Studio is it normal for R.id to access every components

查看:287
本文介绍了R.id访问每个组件是正常的Android Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小的Android Studio(版本2.2.3)应用程序.

I am working on a little Android Studio (version 2.2.3) application.

在添加了包含大量组件的第二个活动之后,我注意到当我在第一个活动中键入R.id时,自动完成功能会向我建议第二个活动中的组件.

After adding a second activity with a lot of components I noticed that when I type R.id in the first activity, the auto-completion proposes me the components from the second activity.

这正常吗?

这是我的问题的一个有效示例(我截取了屏幕截图),为简单起见,我仅创建了两个空的活动,每个活动都有一个按钮.

And here is a working example of my issue (I took the screenshot from it), for simplicity I just created two empty activities each one with a button.

AndroidManifest.xml:

AndroidManifest.xml :

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Main2Activity"></activity>
</application>

MainActivity.java:

MainActivity.java :

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    private Button btMain1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Here is the issue,
        //autocompletion of R.id. shows every layout and their composents
        btMain1 = (Button) findViewById(R.id.); 
    }
}

activity_main.xml:

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapplication.MainActivity">


    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="198dp"
        android:id="@+id/btMain1" />
</RelativeLayout>

Main2Activity.java:

Main2Activity.java :

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {
    private Button btMain2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

activity_main2.xml:

activity_main2.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapplication.Main2Activity">

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="198dp"
        android:id="@+id/btMain2" />
</RelativeLayout>

推荐答案

R.java is a generated file containing all the resource identifiers for your application.

R.id只是一个子类.您还会看到R.layout自动完成,例如在setContentView

R.id is just one subclass. You also would see R.layout auto-complete, for example on setContentView

自动完成将提取所有内容,因为活动,片段,服务等内部没有隔离.

The auto-completion will pull everything because there is no isolation inside of activities, fragments, services, etc.

(我的摘录)

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

public final class R {

    // ...

    public static final class id {
        public static final int action0=0x7f0e0090;
        public static final int action_bar=0x7f0e0060;
        public static final int action_bar_activity_content=0x7f0e0000;
        public static final int action_bar_container=0x7f0e005f;
        public static final int action_bar_root=0x7f0e005b;
        public static final int action_bar_spinner=0x7f0e0001;
        public static final int action_bar_subtitle=0x7f0e0041;

这篇关于R.id访问每个组件是正常的Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆