Android菜单项背景颜色

时间:2017-03-10 18:59:36

标签: android colors menu background android-appcompat

所以我正在寻找一种方法来改变我的菜单弹出背景颜色,但我现在真的没有想法...... 首先,这是我的工具栏:

 <android.support.v7.widget.Toolbar
    android:id="@+id/main_actionbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="2dp"
    app:elevation="2dp"
    app:theme="@style/MainActionBar"
    app:popupTheme="@style/MainActionBar.Popup"/>

和我应用的2个主题没有任何背景更改:

<style name="MainActionBar" parent="Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@color/primary</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>
<style name="MainActionBar.Popup" parent="Widget.AppCompat.PopupMenu">
    <item name="android:textColor">@color/primary_text</item>
</style>

这使我的项目背景颜色看起来是蓝色的,因为我将应用程序的主要颜色和辅助颜色设置为蓝色。

我发现将项目菜单颜色更改为白色的一个解决方案是在我的MainActionBar.Popup中将android:background设置为白色,如下所示:

Widget.AppCompat.PopupMenu with android:background set to @android:color/white

虽然这确实将背景颜色更改为白色,但它也会在动画开始之前渲染弹出白色背后的框,这会破坏动画。

另一个选择是将android:itemBackground设置为白色,如下所示:

Widget.AppCompat.PopupMenu with android:itemBackground set to @android:color/white

正如您所看到的,动画现在看起来很好但项目背景并非完全白色......

我尝试了android:popupBackground但它似乎没有任何效果。

我知道这是一个很多问题,但我已经阅读了很多问题,所以我可能只是因为我没有看到明显的错误...

2 个答案:

答案 0 :(得分:2)

改变这个:

<style name="MainActionBar.Popup" parent="Widget.AppCompat.PopupMenu">
    <item name="android:textColor">@color/primary_text</item>
</style>

使用:

<style name="MainActionBar.Popup" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:colorBackground">Your Background Color</item>
    <item name="android:textColor">Your Text Color</item>
</style>

从MainActionBar主题中删除以下内容:

<item name="android:background">@color/primary</item>

将此属性添加到工具栏标记:

android:background="?attr/colorPrimary"

希望这有帮助。

答案 1 :(得分:0)

要使用深色主题颜色,可以在styles.xml中添加以下行

**Podfile**

platform :ios, '9.0'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'


def flipper_pods()
  flipperkit_version = '0.34.0'
  pod 'FlipperKit', '~>' + flipperkit_version, :configuration => 'Debug'
  pod 'FlipperKit/FlipperKitLayoutPlugin', '~>' + flipperkit_version, :configuration => 'Debug'
  pod 'FlipperKit/SKIOSNetworkPlugin', '~>' + flipperkit_version, :configuration => 'Debug'
  pod 'FlipperKit/FlipperKitUserDefaultsPlugin', '~>' + flipperkit_version, :configuration => 'Debug'
  pod 'FlipperKit/FlipperKitReactPlugin', '~>' + flipperkit_version, :configuration => 'Debug'
end

# Post Install processing for Flipper
def flipper_post_install(installer)
  installer.pods_project.targets.each do |target|
    if target.name == 'YogaKit'
      target.build_configurations.each do |config|
        config.build_settings['SWIFT_VERSION'] = '4.1'
      end
    end
  end
  file_name = Dir.glob("*.xcodeproj")[0]
  app_project = Xcodeproj::Project.open(file_name)
  app_project.native_targets.each do |target|
    target.build_configurations.each do |config|
      cflags = config.build_settings['OTHER_CFLAGS'] || '$(inherited) '
      unless cflags.include? '-DFB_SONARKIT_ENABLED=1'
        puts 'Adding -DFB_SONARKIT_ENABLED=1 in OTHER_CFLAGS...'
        cflags << '-DFB_SONARKIT_ENABLED=1'
      end
      config.build_settings['OTHER_CFLAGS'] = cflags
    end
    app_project.save
  end
  installer.pods_project.save
end



target 'DGFonder' do
  pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
  pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
  pod 'RCTRequired', :path => "../node_modules/react-native/Libraries/RCTRequired"
  pod 'RCTTypeSafety', :path => "../node_modules/react-native/Libraries/TypeSafety"
  pod 'React', :path => '../node_modules/react-native/'
  pod 'React-Core', :path => '../node_modules/react-native/'
  pod 'React-CoreModules', :path => '../node_modules/react-native/React/CoreModules'
  pod 'React-Core/DevSupport', :path => '../node_modules/react-native/'
  pod 'React-RCTActionSheet', :path => '../node_modules/react-native/Libraries/ActionSheetIOS'
  pod 'React-RCTAnimation', :path => '../node_modules/react-native/Libraries/NativeAnimation'
  pod 'React-RCTBlob', :path => '../node_modules/react-native/Libraries/Blob'
  pod 'React-RCTImage', :path => '../node_modules/react-native/Libraries/Image'
  pod 'React-RCTLinking', :path => '../node_modules/react-native/Libraries/LinkingIOS'
  pod 'React-RCTNetwork', :path => '../node_modules/react-native/Libraries/Network'
  pod 'React-RCTSettings', :path => '../node_modules/react-native/Libraries/Settings'
  pod 'React-RCTText', :path => '../node_modules/react-native/Libraries/Text'
  pod 'React-RCTVibration', :path => '../node_modules/react-native/Libraries/Vibration'
  pod 'React-Core/RCTWebSocket', :path => '../node_modules/react-native/'
  pod 'React-cxxreact', :path => '../node_modules/react-native/ReactCommon/cxxreact'
  pod 'React-jsi', :path => '../node_modules/react-native/ReactCommon/jsi'
  pod 'React-jsiexecutor', :path => '../node_modules/react-native/ReactCommon/jsiexecutor'
  pod 'React-jsinspector', :path => '../node_modules/react-native/ReactCommon/jsinspector'
  pod 'ReactCommon/callinvoker', :path => "../node_modules/react-native/ReactCommon"
  pod 'ReactCommon/turbomodule/core', :path => "../node_modules/react-native/ReactCommon"
  pod 'Yoga', :path => '../node_modules/react-native/ReactCommon/Yoga', :modular_headers => true
  pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
  pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
  pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'

  use_native_modules!


  target 'DGFonderTests' do
    inherit! :search_paths
    # Pods for testing
  end
  flipper_pods()
  post_install do |installer|
    flipper_post_install(installer)
    installer.pods_project.targets.each do |target|
      if target.name == 'react-native-config'
        phase = target.project.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
        phase.shell_script = "cd ../../"\
                             " && RNC_ROOT=./node_modules/react-native-config/"\
                             " && export SYMROOT=$RNC_ROOT/ios/ReactNativeConfig"\
                             " && export BUILD_DIR=$RNC_ROOT/ios/ReactNativeConfig"\
                             " && ruby $RNC_ROOT/ios/ReactNativeConfig/BuildDotenvConfig.ruby"

        target.build_phases << phase
        target.build_phases.move(phase,0)
      end
    end

  end
end

现在,在操作栏中设置此主题

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />