Android:android.R.id.content 的用途是什么?

发布于 2024-12-10 11:41:10 字数 273 浏览 2 评论 0原文

任何人都可以解释“android.R.id.content”的含义吗?

它的使用情况如何?

http://developer.android.com 没有任何解释。

public static final int 内容
自:API 级别 1

常量值:16908290 (0x01020002)

Anybody could explain the meaning of "android.R.id.content" ?

How is it being used ?

http://developer.android.com does not have any explanation.

public static final int content
Since: API Level 1

Constant Value: 16908290 (0x01020002)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

超可爱的懒熊 2024-12-17 11:41:10

正如 Philipp Reichart 评论的

android.R.id.content 为您提供视图的根元素,而无需知道其实际名称/类型/ID。查看从当前活动获取根视图

以防万一任何需要的人,您都可以使用 binding 来通过 View Binding 获取此视图.root

As Philipp Reichart commented:

android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID. Check out Get root view from current activity

In case anyone need, you can get this view with View Binding by using binding.root

蓝眼睛不忧郁 2024-12-17 11:41:10

continue

The android.R.id.content ID value indicates the ViewGroup of the entire content area of an Activity.

It can be used with Fragment:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, MyFragment.newInstance())
                .commit();
        }
    }

    ...

}

The code above will insert the View created by Fragment into the ViewGroup identified by android.R.id.content.

亚希 2024-12-17 11:41:10

Google 设计师根据特定或推荐的设计指南来开发 Android UX。 The layout android.R.id.content defines a linearlayout with a few attributes Android believes are a good standard.

因此,使用 android.R.id.content 加载片段管理器的根视图可确保实现这些准则。

注意:此布局设置了属性:android:addStatesFromChildren="true"以允许子片段覆盖此根视图中的属性。

从版本 19 开始,android.R.id.content 在文件中定义:auto_complete_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/content"
    android:orientation="vertical"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:background="@android:drawable/edit_text"
    android:divider="@android:drawable/divider_horizontal_textfield"
    android:addStatesFromChildren="true">

Google designers develop Android UX with specific or recommended design guidelines. The layout android.R.id.content defines a linearlayout with a few attributes Android believes are a good standard.

Thus loading a Fragment Manager's root view with android.R.id.content ensures these guidelines are implemented.

NOTE: This layout has set the attribute: android:addStatesFromChildren="true" to allow child fragments to overwrite attributes in this rootview.

As of version 19, android.R.id.content is defined in a file: auto_complete_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/content"
    android:orientation="vertical"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:background="@android:drawable/edit_text"
    android:divider="@android:drawable/divider_horizontal_textfield"
    android:addStatesFromChildren="true">
给我一枪 2024-12-17 11:41:10

来自片段示例

Snackbar.make(requireContext(), requireActivity().findViewById(android.R.id.content), item.getCategoryName(), Snackbar.LENGTH_SHORT).show();

From Fragment Example

Snackbar.make(requireContext(), requireActivity().findViewById(android.R.id.content), item.getCategoryName(), Snackbar.LENGTH_SHORT).show();
霓裳挽歌倾城醉 2024-12-17 11:41:10

android.R.id.content 在需要视图时非常有用,例如:

Show Snackbar:

Snackbar.make(activity.findViewById(android.R.id.content), MESSAGE, Snackbar.LENGTH_LONG).show();

Fragment transaction

 getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, FragmnetTest.newInstance())
                .commit();

android.R.id.content is very useful for when you need a view, for example:

Show Snackbar:

Snackbar.make(activity.findViewById(android.R.id.content), MESSAGE, Snackbar.LENGTH_LONG).show();

Fragment transaction

 getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, FragmnetTest.newInstance())
                .commit();
笑忘罢 2024-12-17 11:41:10

人们已经给出了一个通用的定义,我想写一下通用的用法。如果您想在扩展至 BottomSheetDialog 或 Dialog 的类中使用小吃栏,通常是“valparentView =dialog?.window?.DecorView?.findViewById(android.R.id.content) ?: binding.root”,您可以需要使用,但“binding.root.rootView”也可以工作,而不是这样做。祝你有美好的一天。

People have made a general definition, and I would like to write about a general usage. If you want to use a snackbar in a class that extends to BottomSheetDialog or Dialog, it's usually "val parentView =dialog?.window?.DecorView?.findViewById(android.R.id.content) ?: binding.root" you may need to make use, but instead of doing this, "binding.root.rootView" will also work. Have a good day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文