Android Studio R.id 访问每个组件是否正常
Android Studio is it normal for R.id to access every components
我正在开发一个 Android Studio(版本 2.2.3)应用程序。
在添加第二个 activity 后,我注意到当我在第一个 activity 中键入 R.id 时,自动完成会向我推荐第二个的组件activity.
这正常吗?
这是我的问题的一个工作示例(我从中截取了屏幕截图),为简单起见,我只创建了两个空活动,每个活动都有一个按钮。
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 :
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 :
<?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 :
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 :
<?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.id
将包含您应用中定义的每个 ID。每个 id 只是一个数字,其中许多不用于任何给定的 activity 或布局。
R.java
是一个包含应用程序所有资源标识符的生成文件。
R.id
只是一个子类。您还会看到 R.layout
auto-complete,例如 setContentView
auto-completion 将提取所有内容,因为活动、片段、服务等内部没有隔离。
(我的片段)
/* 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;
你的问题在我想学习之前就已经被问到了 android。
Android: What is R? Why is it so Cryptic?
R
is a class containing the definitions for all resources of a
particular application package. It is in the namespace of the
application package.
For example, if you say in your manifest your package name is
com.foo.bar
, an R
class is generated with the symbols of all your
resources in com.foo.bar.R
.
There are generally two R
classes you will deal with
- The framework resources in
android.R
and
- Your own in your namespace
It is named R
because that stands for Resources, and there is no
point in making people type something longer, especially since it is
common to end up with fairly long symbol names after it, that can
cause a fair amount of line wrapper.
现在用我的话来说,
如果我们引用您创建的我们自己的资源,我们通常使用 R.
所以 android studio 会根据此给我们建议。
你应该也注意到了android.R
这是为了利用操作系统内置的资源。
所以是的,你会根据你使用的 R
获得建议,现在你应该知道基于 R
的建议不适用于特定的 Activity 或视图,这就是原因它建议所有这些。
还有一个article,您可以获得更多关于R.java
、android.R
和资源的知识。
我正在开发一个 Android Studio(版本 2.2.3)应用程序。
在添加第二个 activity 后,我注意到当我在第一个 activity 中键入 R.id 时,自动完成会向我推荐第二个的组件activity.
这正常吗?
这是我的问题的一个工作示例(我从中截取了屏幕截图),为简单起见,我只创建了两个空活动,每个活动都有一个按钮。
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 :
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 :
<?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 :
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 :
<?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.id
将包含您应用中定义的每个 ID。每个 id 只是一个数字,其中许多不用于任何给定的 activity 或布局。
R.java
是一个包含应用程序所有资源标识符的生成文件。
R.id
只是一个子类。您还会看到 R.layout
auto-complete,例如 setContentView
auto-completion 将提取所有内容,因为活动、片段、服务等内部没有隔离。
(我的片段)
/* 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;
你的问题在我想学习之前就已经被问到了 android。 Android: What is R? Why is it so Cryptic?
R
is a class containing the definitions for all resources of a particular application package. It is in the namespace of the application package.For example, if you say in your manifest your package name is
com.foo.bar
, anR
class is generated with the symbols of all your resources incom.foo.bar.R
.There are generally two
R
classes you will deal with
- The framework resources in
android.R
and- Your own in your namespace
It is named
R
because that stands for Resources, and there is no point in making people type something longer, especially since it is common to end up with fairly long symbol names after it, that can cause a fair amount of line wrapper.
现在用我的话来说,
如果我们引用您创建的我们自己的资源,我们通常使用 R.
所以 android studio 会根据此给我们建议。
你应该也注意到了android.R
这是为了利用操作系统内置的资源。
所以是的,你会根据你使用的 R
获得建议,现在你应该知道基于 R
的建议不适用于特定的 Activity 或视图,这就是原因它建议所有这些。
还有一个article,您可以获得更多关于R.java
、android.R
和资源的知识。