更改弹出菜单项字体大小dynamatically Android

时间:2016-05-02 12:04:46

标签: android menu textview android-menu popupmenu

我正在创建一个Android应用程序,它有一个弹出菜单,单击一个按钮时会打开它,我想动态地增加该弹出菜单中菜单项的文本大小(基于用户输入)。

任何人都可以以编程方式帮助解决这个问题吗?

在我班上:

menuButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Creating the instance of PopupMenu
            PopupMenu popup = new PopupMenu(getActivity(), menuButton);

            //Inflating the Popup using xml file
            popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
            //registering popup with OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.one:
                                 // Some Stuffs
                            break;
                            case R.id.two:
                                // Some Stuffs
                            break;
                            default:
                            break;
                    }
                    return true;
                }
            });

我的xml文件是::

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fav_menu">
<group android:id="@+id/first_section" android:checkableBehavior="none" >
<item
    android:id="@+id/one"
    android:title="@string/sort"
    android:visible="true"

    />
</group>
<group android:id="@+id/first_two" android:checkableBehavior="none">
<item
    android:id="@+id/two"
    android:title="@string/sort"
    android:visible="true"
    />
</group>
</menu>

2 个答案:

答案 0 :(得分:0)

使用SpannableString非常简单:

searchText(name: string){
   this.zoneName = name;
   this.http.get('http://192.168.8.219:8833/zonedata/'+ 
   this.zoneName).subscribe((obj)=>{
      console.log(obj);
   })
}

答案 1 :(得分:-1)

您可以通过将此代码添加到styles.xml来更改文本大小,并在清单文件中使用它。

 <style name="AppTheme" parent="AppBaseTheme">
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
    <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>
    <item name="android:textSize">12sp</item>
</style>


<style name="myPopupMenuTextAppearanceSmall" parent="@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small">
<item name="android:textSize">15sp</item>
</style>
<style name="myPopupMenuTextAppearanceLarge" parent="@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large">  
<item name="android:textSize">25sp</item>
</style>