簡體   English   中英

我找不到任何地方如何從項目菜單更改為所有活動的背景顏色

[英]I could not find anywhere how to change color of background from item menu to all activities

我想創建類似主題的日夜更改。

這是代碼:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId()==R.id.menu_svijetlaTema){
        /**Set background color*/
        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(Color.parseColor("#ffffff"));

        String[] values = getResources().getStringArray(R.array.pitanja);
        final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, values){
            @NonNull
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View a = super.getView(position, convertView, parent);
                TextView boja =  (TextView) a.findViewById(android.R.id.text1);
                boja.setTextColor(Color.BLACK);
                return a;
            }
        };
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_second);
        layout.setBackgroundColor(Color.WHITE);

    } else if(item.getItemId()==R.id.menu_tamnaTema){
        View view = this.getWindow().getDecorView();
        view.setBackgroundColor(Color.parseColor("#4c635c"));

        String[] values = getResources().getStringArray(R.array.pitanja);
        final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, values){
            @NonNull
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View a = super.getView(position, convertView, parent);
                TextView boja =  (TextView) a.findViewById(android.R.id.text1);
                boja.setTextColor(Color.WHITE);
                return a;
            }
        };
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_second);
        layout.setBackgroundColor(Color.BLACK);
    }

return true;
}`

之后,當我單擊菜單應用程序中的項目崩潰時,適配器用於更改ListView ,但是RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_second); layout.setBackgroundColor(Color.WHITE); RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_second); layout.setBackgroundColor(Color.WHITE); 無法正常工作,我正在嘗試從MainActivity菜單將背景顏色更改為MainActivitySecondActivity 這是錯誤:

---------崩潰開始E / AndroidRuntime:致命異常:main進程:hr.itot.vjezbaprvepomoci,PID:2538 java.lang.NullPointerException:嘗試調用虛擬方法void

android.app.Activity.onMenuItemSelected(Activity.java:3204)上位於hr.itot.vjezbaprvepomoci.MainActivity.onOptionsItemSelected(MainActivity.java:605)上的null對象引用上的android.widget.RelativeLayout.setBackgroundColor(int)' .support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:406)位於android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)位於android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper .java:103),位於android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810),位於android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected(AppCompatDelegateImplV9.java:667)。位於android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)的v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)處於android.support.v7.view.menu.MenuPopup處。 android.widget.AdapterView.performItemClick(AdapterView.jav)上的onItemClick(MenuPopup.java:127) a:310),位於android.widget.AbsListView.performItemClick(AbsListView.java:1155),位於android.widget.AbsListView $ PerformClick.run(AbsListView.java:3126),位於android.widget.AbsListView $ 3.run(AbsListView.java: 4041),位於android.os.Handler.dispatchMessage(Handler.java:95),位於android.os.Handler.dispatchMessage(Handler.java:95),位於android.os.Looper.loop(Looper.java:154)。 com的java.lang.reflect.Method.invoke(本機方法)處的app.ActivityThread.main(ActivityThread.java:6077)com.com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) android.internal.os.ZygoteInit.main(ZygoteInit.java:755)`

要更改溢出菜單背景,請將其添加到“活動”主題中

<item name="android:itemBackground">@color/overflow_background</item>

添加清單

  <activity
        android:name="com.alarm.main.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/MyAppActionBarTheme" />

並在values文件夾中創建themes.xml並將其寫下來

<resources>

<style name="MyAppActionBarTheme" parent="@android:style/Theme.Holo">
    <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
    <item name="android:itemTextAppearance">@style/TextAppearance</item>
</style>

<!-- The: background color for Action Bar overflow menu -->
<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
    <item name="android:popupBackground">#0b19ba</item>
</style>

<!-- Popup Menu Text Color styles -->
<style name="TextAppearance" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu"> 
    <item name="android:textColor">#ffffff</item>
</style>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM