mal*_*din 5 android themes android-layout android-theme android-toolbar
注意:我搜索了一个小时并尝试了stackoverflow已经提供的所有解决方案.
我正在研究主题叠加.我制作了一个示例应用程序,它会在单击操作栏图标时打开一个弹出菜单.这是我的styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColorPrimary">@color/colorAccent</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark">
<!-- added all to see which one will work.-->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<item name="android:itemBackground">@color/colorAccent</item>
<item name="android:colorBackground">@color/colorAccent</item>
</style>
<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@color/colorAccent</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是我的工具栏样式.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
我把它设置为popupTheme
我的styles.xml
.现在我想更改弹出菜单的背景颜色,该菜单目前是白色的.
这是代码.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.standard_menu){
showPopupMenu(item);
return true;
}
return super.onOptionsItemSelected(item);
}
private void showPopupMenu(MenuItem item) {
PopupMenu p = new PopupMenu(this, findViewById(item.getItemId()));
p.inflate(R.menu.pop_menu);
p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "clicked.", Toast.LENGTH_SHORT).show();
return true;
}
});
p.show();
}
Run Code Online (Sandbox Code Playgroud)
Kar*_*uri 25
我对接受的答案不满意,因为它没有真正解释为什么OPs自定义弹出式样式没有应用 - 不仅仅是背景,还有文字颜色等 - 所以我做了自己的实验.
重要的是要注意由Toolbar
(当它有菜单项时)创建的弹出窗口与自己显示的弹出窗口之间存在差异PopupMenu
.这些由不同的主题属性控制.另外,请注意有两个PopupMenu
类:android.widget.PopupMenu
和android.support.v7.widget.PopupMenu
.
PopupMenu
您明确显示的样式所需的主题属性是android:popupMenuStyle
或popupMenuStyle
.您有几个选项可以正确应用自定义样式:
(1)android:popupMenuStyle
在活动(或应用程序)的主题中使用
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style/>
<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/popupBackground</item>
</style>
PopupMenu popup = new PopupMenu(this, anchorView);
Run Code Online (Sandbox Code Playgroud)
请注意,在布局文件中不需要额外的内容.
(2)使用ContextThemeWrapper
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- nothing special -->
</style/>
<style name="CustomPopupTheme" parent="ThemeOverlay.AppCompat.Dark">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/popupBackground</item>
</style>
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.CustomPopupTheme);
PopupMenu popup = new PopupMenu(ctw, anchorView);
Run Code Online (Sandbox Code Playgroud)
注意R.style.PopupMenu
在构造时不直接使用它ContextThemeWrapper
.这看起来有点迂回,但是如果你想让弹出主题与活动或应用主题分开(例如,可能只有一些弹出窗口需要你的特殊主题),它会很有用.
(3)使用你AppBarLayout
的背景
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- nothing special -->
</style/>
<style name="AppBarOverlay" parent="ThemeOverlay.AppCompat.Light">
<!-- if using android.widget.PopupMenu -->
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<!-- if using android.support.v7.widget.PopupMenu -->
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/popupBackground</item>
</style>
<style name="PopupOverlay" parent="ThemeOverlay.AppCompat.Dark">
<!-- changes the background of the Toolbar's popup -->
<item name="android:colorBackground">@color/popupBackground</item>
</style>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
AppBarLayout appBar = (AppBarLayout) findViewById(R.id.app_bar);
PopupMenu popup = new PopupMenu(appBar.getContext(), anchorView);
Run Code Online (Sandbox Code Playgroud)
由于您已经拥有AppBar的主题叠加层,因此您可以使用它来保存弹出式主题参考.这也适用于工具栏的上下文,至少考虑到当前的布局,虽然注意app:popupTheme
这里实际上并不相关,因为它会影响Toolbar
弹出窗口,而不是你的弹出窗口PopupMenu
.还要注意这与上面的选项2有多相似,这应该让你了解android:theme
属性如何在引擎盖下工作;)
在我的实验中,android:itemBackground
只有当我用来代替它的工作android:colorBackground
的PopupOverlay
风格.但是,最好使用它,android:colorBackground
因为这会改变弹出窗口的颜色,使圆角和可选项突出显示/纹理完整.
<style name="YOURSTYLE" parent="Widget.AppCompat.PopupMenu">
<item name="android:textColor">@android:color/white</item>
<item name="android:itemBackground">@android:color/holo_red_light</item>
</style>
Context wrapper = new ContextThemeWrapper(this, R.style.YOURSTYLE);
PopupMenu popup = new PopupMenu(wrapper, view);
Run Code Online (Sandbox Code Playgroud)
可能对你有帮助
归档时间: |
|
查看次数: |
11231 次 |
最近记录: |