简体   繁体   中英

changing text size on popupmenu android

I have a popup menu that works. The user would press an image and a popup menu appears with 5 items. The problem is that I don't seem to be able to change the size of the text of the popup. The Java that calls the popup is as follows:

     public void TheCompanyMenu(View v) {
     PopupMenu mypopupmenu = new PopupMenu(this, v); 
     mypopupmenu.setOnMenuItemClickListener(this);  
     MenuInflater inflater = mypopupmenu.getMenuInflater(); 
     inflater.inflate(R.menu.popup, mypopupmenu.getMenu());
     mypopupmenu.show();
        }

        @Override 
        public boolean onMenuItemClick(MenuItem arg0) {
        switch (arg0.getItemId()) {  
        case R.id.option1:
           Intent intent1 = new Intent(this, MainActivity.class);
           startActivity(intent1);
        return true;
        case R.id.option2:
            Intent intent2 = new Intent(this, Item2.class);
            startActivity(intent2);
        return true;
        case R.id.option3:
            Intent intent3 = new Intent(this, Item3.class);
            startActivity(intent3);
        return true;    
        case R.id.option4:
            Intent intent4 = new Intent(this, Item4.class);
            startActivity(intent4);    
        return true;
        case R.id.option5:
        Intent intent5 = new Intent(this, Item5.class);
            startActivity(intent5);        
        return true;
        default:
        return super.onContextItemSelected(arg0);
        }
        }

The Menu xml called popup is as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/option1"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/HomeMenu"
     android:title="@string/HomeMenu" />

     <item android:id="@+id/option2"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/option2"
     android:title="@string/option2menu" />

     <item android:id="@+id/option3"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/option3"
     android:title="@string/option3menu" />

     <item android:id="@+id/option4"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/option4"
     android:title="@string/option4menu" />

     <item android:id="@+id/option5"
     style="@style/PopupItemStyle"
     android:textSize="12sp"
     android:text="@string/option5"
     android:title="@string/option5Menu" />
     </menu>

I have tried changing the android:textSize to be dp but it has no effect.

The code in the style xml is:

 <style name="PopupItemStyle"> 
    <item name="android:background">#FFA0A0A0</item>
    <item name="android:gravity">center</item>
    <item name="android:padding">10dp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">12sp</item>
 </style>

As I am fairly new to Java, I am keen to keep the java bit as it is (ie using popupmenu) as it is working (sort of), but need to change the text size. Thanks a lot!

actually,I also want do like this.but,I do it in another way.

private void showPopUp(View v) {
PopupMenu popup = new PopupMenu(DetailActivity.this, v);
LayoutInflater inflater = (LayoutInflater) DetailActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.GRAY);

//link layout and popwindow
View myView  = inflater.inflate(R.layout.menu_popwindow, null);
PopupWindow myPopupWindow = new PopupWindow(myView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
myPopupWindow.setFocusable(true);//getfocus for editview        
//the follow help dismiss popup     myPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.linkme_menu));  
myPopupWindow.setOutsideTouchable(true);
myPopupWindow.setOnDismissListener(new OnDismissListener() { 
    @Override  
    public void onDismiss()  
    {  }   }});myPopupWindow.showAsDropDown(v);

i use it in actionbar,you can creat a new xml(menu_popwindow)by yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM