[英]Why isn't my style being applied to my popupMenu?
According to How to style PopupMenu? 根据如何设置PopupMenu的风格? you cannot set a popupmenu style directly, which seems contrary to https://developer.android.com/reference/android/widget/PopupWindow.html 你不能直接设置popupmenu样式,这似乎与https://developer.android.com/reference/android/widget/PopupWindow.html相反
It states you can specify a style in the PopupMenu constructor. 它声明您可以在PopupMenu构造函数中指定样式。 According to http://blog.http417.com/2014/06/styling-popupmenu.html it looks like the attributes I need to specify are "android:popupBackground" and "android:dropDownWidth". 根据http://blog.http417.com/2014/06/styling-popupmenu.html看起来我需要指定的属性是“android:popupBackground”和“android:dropDownWidth”。 However, the following just removes the default style and doesn't apply my desired color or width: 但是,以下只是删除默认样式,并不应用我想要的颜色或宽度:
styles.xml: styles.xml:
<style name="popup">
<item name="android:popupBackground">@color/material_blue_grey_800</item>
<item name="android:dropDownWidth">350dp</item>
</style>
ActivityA.java ActivityA.java
public class ActivityA extends AppCompatActivity implements
PopupMenu.OnMenuItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
findViewById(R.id.popupBtn).setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View button) {
PopupMenu popupMenu = new PopupMenu(ActivityA.this,
button, Gravity.RIGHT, 0, R.style.popup);
}
}
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
}
activity_a.xml: activity_a.xml:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".activities.ActivityA">
<Button
android:id="@+id/popupBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="Press to open menu"/>
</RelativeLayout>
您不能以编程方式应用样式,但可以使用主题执行: https : //developer.android.com/guide/topics/ui/themes.html#ApplyAStyle可以在此处找到类似的答案: https:// stackoverflow。 COM / A /九十三万六千二百六十九分之二百零一万六千三百四十四
Add this to your AppTheme .. 将此添加到您的AppTheme ..
<item name="android:popupMenuStyle">@style/popup</item>
<item name="popupMenuStyle">@style/popup</item>
Good luck! 祝好运!
You can declare the styles in your theme like below: 您可以在主题中声明样式,如下所示:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Other styles -->
<!-- PopUpMenu styles -->
<item name="popupMenuStyle">@style/Widget.App.PopupMenu</item>
</style>
<!-- Widget PopUpMenu Style-->
<style name="Widget.App.PopupMenu" parent="Widget.AppCompat.Light.PopupMenu">
<!-- Whatever Styles Put Here-->
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.