在popupmenu android上更改文本大小

时间:2013-12-28 16:56:55

标签: java android popup

我有一个可用的弹出菜单。用户将按下图像,弹出菜单会显示5个项目。问题是我似乎无法更改弹出文本的大小。 调用弹出窗口的Java如下:

     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);
        }
        }

名为popup的Menu xml如下:

    <?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>

我已经尝试将android:textSize更改为dp,但它没有效果。

xml样式中的代码是:

 <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>

由于我是Java的新手,我很想保持java位(即使用popupmenu),因为它正在工作(有点),但需要更改文本大小。 非常感谢!

1 个答案:

答案 0 :(得分:0)

实际上,我也想这样做。但是,我是以另一种方式做到的。

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);

我在动作栏中使用它,你可以自己创建一个新的xml(menu_popwindow)。