use*_*216 5 android styles background popupmenu
我正在尝试将样式应用于Android弹出菜单.按钮单击时显示菜单.在我的例子中,我想设置黑色菜单背景.
那么,我的菜单布局:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Item 1"
android:id="@+id/menu1">
</item>
<item
android:title="Item 2"
android:id="@+id/menu2">
</item>
</menu>
Run Code Online (Sandbox Code Playgroud)
接下来,我的活动布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.michal.popupmenu.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="btnClick"
android:nestedScrollingEnabled="true"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
单击按钮时显示菜单的代码:
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnClick(View view)
{
showMenu(view);
}
public void showMenu(View v)
{
PopupMenu popup = new PopupMenu(this, v);
popup.inflate(R.menu.popup_menu);
popup.show();
}
}
Run Code Online (Sandbox Code Playgroud)
样式xmle是自动生成的.我只添加了菜单样式来设置黑色菜单背景,这里是:
<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:popupBackground">@android:color/black</item>
</style>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="popupMenuStyle">@style/PopupMenu</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但仍然菜单背景为白色,应该是黑色.有什么想法有什么不对吗?
[编辑]根据评论,更新代码:
<resources>
<style name="PopupMenu" parent="@style/Widget.AppCompat.Light.PopupMenu">
<item name="android:popupBackground">#FF4081</item>
</style>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
主要活动:
public void showMenu(View v)
{
Context wrapper = new ContextThemeWrapper(getApplicationContext(), R.style.PopupMenu);
PopupMenu popup = new PopupMenu(wrapper, v);
// This activity implements OnMenuItemClickListener
//popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) this);
popup.inflate(R.menu.popup_menu);
popup.show();
}
Run Code Online (Sandbox Code Playgroud)
结果不是我所期望的
菜单背景仍然是黑色,而不是我想要设置的颜色.
我也试过上面提到的解决方案,但我的popupmenu颜色没有改变,所以我最终做了如下:
1.处理具有所需颜色的定制抽屉,如下所示:
popup_color_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color
android:color="@color/colorPrimary"/>
</item>
<item>
<color
android:color="#655611"/>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
添加state_pressed以获得选择效果
2.在我的styles.xml,添加以下代码:
<style name="MyPopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:itemBackground">@drawable/popup_color_drawable</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我Theme.AppCompat.Light.DarkActionBar用作app的基本主题.
然后在我的活动中
public void showpopup(View view){
Context wrapper = new ContextThemeWrapper(this, R.style.MyPopupMenu);
PopupMenu popup = new PopupMenu(wrapper, view);
popup.inflate(R.menu.popup_menu);
popup.show();
}
Run Code Online (Sandbox Code Playgroud)
希望这对你有所帮助.
你应该尝试这个:
public void showMenu(View v)
{
Context wrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenu);
PopupMenu popup = new PopupMenu(wrapper, v);
popup.inflate(R.menu.popup_menu);
popup.show();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6919 次 |
| 最近记录: |