android.R.id.content作为容器的碎片 [英] android.R.id.content as container for Fragment

查看:2761
本文介绍了android.R.id.content作为容器的碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是活动A,其中包含片段B.我始终贯彻像这样。

My situation is Activity A which contains Fragment B. I always implement it like this.

布局活动答:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

布局片段B:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_title"
        android:layout_centerInParent="true"
        android:background="@drawable/green_button"
        android:textColor="@android:color/white"/>

</RelativeLayout>

这个伟大的工程,但是,如果我们打开Android设备监控,并期待在视图层次:

This works great, but if we open Android Device monitor and look at View Hierarchy:

所以,我不喜欢在我的层次结构有两个相同没用FrameLayouts,我可以把我R.id.container。我这样做,是这样的:

So, I do not like that in my hierarchy there are two same useless FrameLayouts and I can cut my R.id.container. I do it like this:

的onCreate(捆绑参数)在我的活动A实现:

onCreate(Bundle args) implementation in my Activity A:

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

    getFragmentManager().beginTransaction()
        .add(android.R.id.content, FragmentB.newInstance()).commit();
}

我只是不设置我的活动内容和附上我的片段B系统容器android.R.id.content。这对我的伟大工程。我删除了一个无用的包括。

I just do not set content for my Activity and attach my Fragment B to system container android.R.id.content. This works great for me. I removed one useless include.

我的问题是它很好的做法,这样做黑客。莫非crashs我的应用程序在任何情况下,我会这样实施后有什么问题呢?可能是有人在这个问题的有益经验?

My question is it good practice to do this "hack". Could it crashs my application in any cases and what problems could I have after this implementation? May be somebody has useful experience in this question?

感谢所有好的答案。

推荐答案

有什么不妥的地方。就像你说:你不需要你的额外R.id.content布局,使...只是不添加的setContentView它。甚至有提及关于它的ActionBar的正式文件:<一href=\"http://developer.android.com/guide/topics/ui/actionbar.html#Tabs\">http://developer.android.com/guide/topics/ui/actionbar.html#Tabs

there is nothing wrong with it. Like you said: you dont need your extra R.id.content layout so... just don't add it with setContentView. There is even mention about it in official documentation of ActionBar: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs

可选地,如果选项卡内容将填充活性布局然后,
  你的活动并不需要在所有的布局(你甚至都不需要
  调用的setContentView())。相反,你可以放置在每一个片段
  默认的根视图,你可以参考同
  android.R.id.content ID

Alternatively, if the tab content will fill the activity layout, then your activity doesn't need a layout at all (you don't even need to call setContentView()). Instead, you can place each fragment in the default root view, which you can refer to with the android.R.id.content ID

如果你只开发14+(因为土生土长的动作条的)一切都应该与它的罚款,但如果您使用支持的lib请阅读以下要点。

If you develop only for 14+ (because of native ActionBar) everything should be fine with it, but if you use support lib please read the points below.

重要的是:什么是您为您的开发API分的水平?



如果您的应用程序支持的API&LT; 14并使用应用程序兼容性,你必须知道不同的行为。
android.R.id.content 是屏幕,在这里你的应用程序应该显示它的内容的一部分。
在原生API 14+这是下面的ActionBar只是一部分,因为这部分应该显示活动内容。



在应用程序兼容性,那里是没有的ActionBar原生支持。 android.R.id.content 是整个应用程序屏幕的容器。这意味着 - 包括动作条,因为动作条进行了仿真那里添加为标准视图层次结构。为了解决这个问题,你必须检查你是否是在API超过14下,并使用不同的ID: R.id.action_bar_activity_content

Important thing is: What is your min API level that you develop for?

If your app supporting API < 14 and you use AppCompat you must be aware the different behavior. android.R.id.content is the part of screen where your application should display it's content. On native API 14+ This is just part below ActionBar, because this part is supposed to display activity content.

In AppCompat, where there is no native support for ActionBar. android.R.id.content is the container of entire app screen. This means - including ActionBar, because ActionBar is emulated there and added as a standard view hierarchy. To solve this issue you have to check whether you are on API lower than 14 and use different id: R.id.action_bar_activity_content



您可以创建helper方法得到正确ID:


You can create helper method to get correct id:

public static int getContentViewId() {
    return Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH ? android.R.id.content : R.id.action_bar_activity_content;
}

所以,如果你是为14+开发这个是完全没有问题的解决方案。如果使用自定义动作条的实施(如应用程序兼容性),你必须做这一招。

So if you are developing for 14+ this is perfectly fine solution. If you use custom ActionBar implementation (like AppCompat) you have to do this trick.

看来,这种行为固定在支持库修订19:
<一href=\"https://$c$c.google.com/p/android/issues/detail?id=58108#c21\">https://$c$c.google.com/p/android/issues/detail?id=58108#c21

It seems that this behavior was fixed in Support Library revision 19: https://code.google.com/p/android/issues/detail?id=58108#c21

<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/support/v7/app/ActionBarActivityDelegateBase.java/#228\">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.2_r1/android/support/v7/app/ActionBarActivityDelegateBase.java/#228

您可以看到,他们与标准的新老交替 R.id.action_bar_activity_content android.R.id.content (和老android.R.id.content与NO_ID)为更好的兼容性!所以,如果你使用支持库R19或更高(或只是一个本地框架),你可能才刚刚android.R.id.content两个&LT; 14和14+变种:)

You can see that they replacing the old R.id.action_bar_activity_content with standard android.R.id.content (and the old android.R.id.content with NO_ID) for better compatibility! So if you use Support Lib r19 or greater (or just a native framework) you can just just android.R.id.content in both <14 and 14+ variants:)

这篇关于android.R.id.content作为容器的碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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