更改动作条从内部片段叠加片段、动作

2023-09-07 08:39:02 作者:十斤情话不敌一个拥抱

我有个片段(片段1),该被替换为另一片段(片段2)。片段1被放置在堆栈中。我使用的是兼容模式(不ActionBarSherlock)。

下面是我的问题。我想的动作条在某些片段被显示为覆盖,而不是在其他。具体而言,当片段2所示,我希望它出现在覆盖,然后再回到曾经片段2退出正常的动作条​​。

片段1有一个常规动作条,始终是可见的。但是,当我更换1片段与片段2,我需要5秒后隐藏的动作条。如果存在触摸事件时,动作条再次示出。这一切工作正常,但是,片段2的动作条被隐藏或显示每次重绘。正因为如此,我想在片段2显示的动作条为覆盖。

我知道我可以改变动作条覆盖,但我不知道该怎么做编程的片段中。我不想改变它的每一个片段,只是片段2。

思路?????

解决方案

这可能不是你所期望的答案。

考虑一个不同的问题:我们能否改变后的setContentView活动的主题(...)已叫什么名字?这个问题已经被问了很多次,和一个共同的解决办法是重新创建(调用完成() startActivity(getIntent()))的活动,并设置前的setContentView新的主题(...)

在Word文档里怎样把一段文字改成蓝色背景

您的问题是一个扩展,这一点 - 添加了从片段更改主题的复杂性。在任何情况下,我不认为上面的一个很好的提到的解决方案。

的ActionBar是第一部件之一在创建活动时被初始化。我不认为你会找到一个方法以某种方式'刷新'它使用新属性。请参阅下面如何使用 requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY)方法处理后的setContentView(...)调用:

  @覆盖
公共布尔requestFeature(INT FEATUREID){
    如果(mContentParent!= NULL){
        抛出新AndroidRuntimeException(requestFeature()必须
                                                       所谓的添加内容)之前;
    }
    ....
    ....
}
 

所以,如果的setContentView(...)已呼吁活动(这是,你的情况),运行时,会抛出异常。

是否有可能,你甚至不需要此功能?

开始通过设置动作条是在你的主题覆盖:

 <项目名称=机器人:windowActionBarOverlay>真< /项目>
<! - 支持库属性的兼容性 - >
<项目名称=windowActionBarOverlay>真< /项目>
 

  

下面是我的问题。我想在动作条在某些片段显示为叠加...

好。我们已经调配了这上面。

  

...而不是在别人。

说你不希望有动作条在片段B.覆盖然后,在片段B的布局,请执行以下操作:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / drawer_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_marginTop =机器人:ATTR / actionBarSize> <<<  -  ATTR / actionBarSize
                                                                    兼容性

    ....
    ....
< / LinearLayout中>
 

通过顶缘设置为ActionBar的尺寸,片段B看起来好像它具有规则的动作条 - 不重叠的之一。另一种方式来实现,这将是有一个查看安卓layout_height =机器人:ATTR / actionBarSize置于作为第一子在上面的布局。

在本质:

您的动作条将是一个覆盖。 在那里的动作条会自动隐藏,碎片布局的碎片将不会有任何顶边距设置。 在那里的动作条不应被覆盖的片段,该片段布局的会有顶边距设置为 actionBarSize

一个点钞(感谢耶勒

如果你的动作条是半透明的,这将是最好用填充,而不是保证金一致的外观。

I have a fragment (Fragment 1) that is replaced by another fragment (Fragment 2). Fragment 1 is placed on the stack. I'm using compatibility mode (not ActionBarSherlock).

Here's my problem. I want the actionbar to be displayed as overlay in some fragments, but not in others. Specifically, when Fragment 2 is shown, I want it to appear in overlay and then go back to the normal actionbar once Fragment 2 exits.

Fragment 1 has an regular actionbar that is always visible. But, when I replace Fragment 1 with Fragment 2, I need to hide the actionbar after 5 seconds. If there is a touch event, the actionbar is shown again. This all works fine, but, Fragment 2 is redrawn each time the actionbar is hidden or revealed. Because of this, I want to make the actionbar in Fragment 2 show as an overlay.

I know I can change the actionbar overlay but I don't know how to do that programmatically from within the fragment. I DON'T want to change it for every fragment, just Fragment 2.

Ideas?????

解决方案

This may not be the answer you were hoping for.

Consider a different question: Can we change activity theme after setContentView(...) has been called? The question has been asked many times, and a common solution is to recreate(calling finish() and startActivity(getIntent())) the activity and set the new theme before setContentView(...).

Your question is an extension to this - with added complexity of changing the theme from a fragment. In any case, I don't consider the solution mentioned above a good one.

ActionBar is one of the first components to be initialized when an Activity is created. I don't think you will find a way to somehow 'refresh' it with new attributes. See below how the requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY) method deals with post-setContentView(...) calls:

@Override
public boolean requestFeature(int featureId) {
    if (mContentParent != null) {
        throw new AndroidRuntimeException("requestFeature() must be 
                                                       called before adding content");
    }
    ....
    ....
}

So, if setContentView(...) has already been called for the Activity (which it is, in your case), a runtime-exception will be thrown.

Is it possible that you don't even require this functionality?

Start by setting the ActionBar to be an overlay in your theme:

<item name="android:windowActionBarOverlay">true</item>
<!-- Support library attribute for compatibility -->
<item name="windowActionBarOverlay">true</item>

Here's my problem. I want the actionbar to be displayed as overlay in some fragments...

Okay. We have already provisioned for this above.

... but not in others.

Say you don't want to have the ActionBar as an overlay in Fragment B. Then, in Fragment B's layout, do the following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize" >  <<<-- ?attr/actionBarSize
                                                                    for compatibility

    ....
    ....
</LinearLayout>

With the top-margin set to ActionBar's size, Fragment B looks as if it has a regular ActionBar - not an overlaid one. Another way to achieve this would be to have a View with android:layout_height="?android:attr/actionBarSize" placed as the first child in the layout above.

In essence:

your ActionBar will be an overlay. in the fragments where the ActionBar will auto-hide, the fragments layout will not have any top-margin set. in the fragments where the ActionBar should not be overlaid, the fragments layout will have top-margin set to actionBarSize.

A point of note (thanks to Jelle):

If your ActionBar is semi-transparent, it would be best to use padding instead of margin for a consistent look.