简体   繁体   English

Android在ThemeOverlay.AppCompat.Dark.ActionBar中将工具栏/小部件背景更改为黑色

[英]Android Change Toolbar/Widget Background to Black in ThemeOverlay.AppCompat.Dark.ActionBar

动作栏菜单必须保持灰色 弹出菜单必须保持灰色 与应用具有相同背景的小部件,需要小部件为黑色

Hi I just designed a custom widget with a popup menu. 嗨,我刚刚设计了一个带有弹出菜单的自定义窗口小部件。 This means by default I have to extend AppCompatActivity and am forced to use Theme.AppCompat.NoActionBar or ThemeOverlay.AppCompat.Dark or ThemeOverlay.AppCompat.Dark.ActionBar 这意味着默认情况下,我必须扩展AppCompatActivity并被迫使用Theme.AppCompat.NoActionBar或ThemeOverlay.AppCompat.Dark或ThemeOverlay.AppCompat.Dark.ActionBar

The thing is, I would like to have the widget that I used to replace the action bar black and keep the popup menu grey and the menu that comes up by pressing the device action menu grey. 问题是,我想拥有用来替换操作栏的小部件,并保持弹出菜单为灰色,并通过按设备操作菜单将其显示为灰色。 Could anyone please post sample code to accompany a description of how to do this. 任何人都可以张贴示例代码,并附上有关如何执行此操作的说明。 At this stage telling me how to will not help because I have tried using java and changing primary and dark colors in the theme. 在此阶段,告诉我如何操作将无济于事,因为我尝试使用Java并更改主题中的原色和深色。 I also tried adding multiple custom styling in styles.xml which I ended up commenting out and eventually deleting. 我还尝试在styles.xml中添加多个自定义样式,最终将其注释掉并最终删除。 Please see my code below: 请在下面查看我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp" tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000"
    android:elevation="5dp"
    android:titleTextColor="#fff"
    android:layout_alignParentTop="true"
    android:title="@string/app_name"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
   />

package com.example.android.widget;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewParent;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        myToolbar.setTitle("Widget");
        myToolbar.setTitleTextColor(0xFFFFFFFF);
        myToolbar.setBackgroundColor(Color.rgb(51, 51, 51));
        View titleView = getWindow().findViewById(android.R.id.title);
        if (titleView != null) {
            ViewParent parent = titleView.getParent();
            if (parent != null && (parent instanceof View)) {
                View parentView = (View)parent;
                parentView.setBackgroundColor(Color.rgb(51, 51, 51));
            }
        }
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    // Menu options to set and cancel the alarm.
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // When the user clicks START ALARM, set the alarm.
            case R.id.start_action:

                return true;
            // When the user clicks CANCEL ALARM, cancel the alarm.
            case R.id.cancel_action:

                return true;
        }
        return false;
    }
}


<resources><!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:textColor">#fff</item>
    <item name="android:actionMenuTextColor">#fff</item>
    <item name="colorPrimary">@color/colorBackground</item>
    <item name="colorPrimaryDark">@color/black</item>
</style>

    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="@+id/start_action"
    android:orderInCategory="100"
    android:title="@string/start_text"
    app:showAsAction="never"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    />
<item android:id="@+id/cancel_action"
    android:orderInCategory="100"
    android:title="@string/cancel_text"
    app:showAsAction="never"
    />

As far as i know you dont need to extend the AppCompactActivity because you are using a custom Toolbar. 据我所知,您不需要扩展AppCompactActivity,因为您使用的是自定义工具栏。 I came accross the same issue last months and the way i solved it was to get rid of the AppCompactActivity extention and put this instead: 上个月,我遇到了相同的问题,而解决问题的方法是摆脱AppCompactActivity扩展,而改为:

public class MainActivity extends Activity

and to see how the app would look while styling and coding it you need to choose the preview to AppNoTitleBarFullScreen. 并查看样式和编码时该应用的外观,您需要选择AppNoTitleBarFullScreen的预览。

also on your OnCreate() method you need to add the following: 同样在OnCreate()方法上,您需要添加以下内容:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.Your_Activity);
        ...
}

And on your styles XML file add the following: 并在样式XML文件上添加以下内容:

<style name="Theme.AppCompat.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.NoTitleBar.FullScreen">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item> 
</style>

I belive thats it!... please anybody correct me if i am wrong 我相信这就是!...如果我错了,请任何人纠正我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM