android - 主题不适用于 Fragment

标签 android android-fragments

我正在尝试在 fragment 上应用特定主题。但由于某种原因,它没有发生。谁能指出我代码中的错误?或者更好的解决方案?

styles.xml 中的主题:

<!-- Theme for trans actionbar -->
<style name="TransTheme" parent="@style/AppTheme">
    <item name="android:actionBarStyle">@style/TransActionbar</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="actionBarStyle">@style/TransActionbar</item>
    <item name="windowActionBarOverlay">true</item>
</style>

@style/TransActionbar 在哪里:

<!-- Actionbar style for trans theme -->
<style name="TransActionbar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/first_trans_actionbar</item>
    <item name="background">@color/first_trans_actionbar</item>
</style>

我想如何在 onCreateView() 方法的 fragment 中应用主题:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //set theme
    final Context contextWrapper = new ContextThemeWrapper(getActivity(), R.style.TransTheme);
    LayoutInflater localInflater = inflater.cloneInContext(contextWrapper);
    contentView = localInflater.inflate(R.layout.fragment_movie_detail,
            container, false);
    return contentView:
}

出于某种原因, fragment 保留了旧主题..

编辑

所以基本上问题归结为:如何在 Activity 的一个 fragment 中使 Actionbar 透明(和覆盖模式)?

最佳答案

我猜你想切换两个(或更多)Fragment s 在同一个Activity ,其中每个人都可能需要 ActionBar是否被覆盖,并根据当前的不同背景有不同的Fragment .

让我们先解决最重要的问题:到目前为止,您还不能更改 Activity运行时的主题。

您仍然可以通过执行以下操作相对轻松地完成您所追求的目标:

  1. 设置Activity使用覆盖的主题 ActionBar .
  2. 对于那些Fragment这是你不想要 ActionBar 的地方要覆盖,在他们的布局 xml 中,设置顶部填充/边距等于 ActionBar高度。理想情况下,您只需引用 the dimension resource for action bar height .对于那些Fragment这是你想要 ActionBar 的地方要覆盖它们,您应该在它们的布局 xml 顶部没有填充/边距。
  3. 当你切换Fragment s,更改ActionBar的背景颜色调用getActionBar() (或 getSupportActionBar 如果您使用的是支持库)然后是 .setBackgroundColor(Color.TRANSPARENT)或者使用 getResources().getColor(R.colors.some_color) 从资源中需要的任何颜色.你可以通过动画颜色变化来获得比突然切换更奇特的东西,但这超出了这个问题的范围。
  4. ...

  5. 利润!

编辑:使用 Toolbar

如果你想使用 Toolbar ,方法是将它放在一个单独的布局 xml 中,您 <include>在你的里面Activity layout 并给它一些你可以通过代码引用的 ID。确保 Activity根布局是 Z 排序的东西,比如 RelativeLayoutFrameLayout , 并定位工具栏 <include>在 Y 轴的顶部(alignParentTop 表示 RelativeLayoutlayout_gravity="top" 表示 FrameLayout),Toolbar <include>在任何布局之后你都会把你的 Fragment s 在里面,这样它就会覆盖它们。

Activity里面onCreate这样做:

Toolbar yourToolbar = (Toolbar) findViewById(R.id.your_toolbar);
yourToolbar.setBackgroundColor(<whatever color you want>);
setSupportActionBar(yourToolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true/false);
actionBar.setDisplayHomeAsUpEnabled(true/false);
actionBar.setTitle(<whatever title you want>);

否则,我原来的答案中的所有内容仍然有效。

关于android - 主题不适用于 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29952179/

相关文章:

android - Android 中的 PlaceholderFragment

java - 我收到警告 "App is not indexable by Google Search; consider adding at least one Activity with an ACTION-VIEW intent filter. "

java - 尝试使用 Delphi 更改 Android 设备 R/W 权限时发生访问冲突

android - 如何设置随机颜色

android - 如何重新加载/刷新已经显示过一次的 fragment ?

java - fragment 内容未出现在手机上

java - 如何在Android上的fragment中使用onBackPress按钮

android - 设置 Cardview 形状和背景半透明

php - PayPal - 无法使用交易 ID (GetTransactionDetails API) 验证付款

android - 网络连接可以在省电模式下停止,并出现 java.net.SocketTimeoutException