更改弹出菜单大小

时间:2017-12-24 20:35:36

标签: java android popupmenu

目前,我正在尝试在ImageButton上创建一个弹出菜单,虽然我希望菜单的大小(宽度)更小,但是并排排列它们?但这不是主要的问题,它是大小(宽度)我想减少png或只是单词本身。我希望你们能帮助我解决这个问题。我花了3天时间搞清楚菜单。另外,我尝试添加一些填充和android:width / android:layout_width。这些都没有奏效,我希望能解决这个问题。

Main.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;



 public class MainActivity extends AppCompatActivity
{
public WebView webView;
public EditText search_bar;
private ImageButton options_menu;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webView);

    //Open in Equinox instead of Deafult Browser
    webView.setWebViewClient(new
            WebViewClient());

    //WebView - JavaScript-WebViewSettings-HomePage
    final WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.getSettings().setSupportZoom(true);
    webView.loadUrl("https://www.Google.ca");

    //OptionsBtn
    options_menu = (ImageButton) findViewById(R.id.options_menu);
    options_menu.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Creating the instance of PopupMenu
            PopupMenu popup = new PopupMenu(MainActivity.this, options_menu);
            //Inflating the Popup using xml file
            popup.getMenuInflater()
                    .inflate(R.menu.popup_menu, popup.getMenu());

            //registering popup with OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.back:
                            if(webView.canGoBack()){
                                webView.goBack();
                            }
                            else{
                                webView.reload();
                            }
                            return true;
                        case R.id.forward:
                            if(webView.canGoForward()){
                                webView.goForward();
                            }
                            else{
                                webView.reload();
                            }
                            return true;
                        case R.id.refresh:
                            webView.reload();
                        default:
                            return false;
                    }
                }
            });

            popup.show(); //showing popup menu
        }
    }); //closing the setOnClickListener method

    //"Go" on Keyboard with search
    search_bar=(EditText)findViewById(R.id.search_bar);
    search_bar.setOnEditorActionListener(new OnEditorActionListener(){
        @Override
        public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
            if(arg1 == EditorInfo.IME_ACTION_GO)
            {
                if (search_bar.getText().toString().contains(".com")) {
                    webView.loadUrl("https://".concat(search_bar.getText().toString()));
                } else {
                    webView.loadUrl("https://www.google.ca/search?q=".concat(search_bar.getText().toString()));
                }
            }
            return false;
        }
    });
}
public void onBackPressed(){
    if (true){
        webView.goBack();
    }
    else{
        webView.reload();
    }

}
}

popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">


<item
    android:id="@+id/back"
    android:title="@string/back" />
<item
    android:id="@+id/forward"
    android:title="@string/forward" />
<item
    android:id="@+id/refresh"
    android:title="@string/refresh" />

1 个答案:

答案 0 :(得分:1)

我可以借助以下代码减少菜单项的高度和文字大小:

<强> RES /值/ styles.xml:     

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">

        <item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>
        <item name="android:actionBarWidgetTheme">@style/PopupMenuTextView</item>
        <item name="android:popupMenuStyle">@style/PopupMenu</item>

        <!-- Change Overflow Menu ListView Item Height & Property -->
        <item name="android:listPreferredItemHeightSmall">10dp</item>
        <item name="android:listPreferredItemPaddingLeft">5dp</item>
        <item name="android:listPreferredItemPaddingRight">5dp</item>
    </style>

    <!-- Change Overflow Menu ListView Divider Property -->
    <style name="PopupMenuListView" parent="@android:style/Widget.Holo.ListView.DropDown">
        <item name="android:divider">#FF0000</item>
        <item name="android:dividerHeight">2dp</item>
    </style>

    <!-- Change Overflow Menu ListView Text Size and Text Size -->
     <style name="PopupMenuTextView" parent="@style/android:Theme.Holo.Light">
        <item name="android:textColor">#00FF00</item>
        <item name="android:textSize">5sp</item>
    </style>

     <!-- Change Overflow Menu Background -->
     <style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">#888888</item>
    </style>

</resources>

在styles.xml(v11)和styles.xml(v14)中添加上述代码。

更多详情:

参考Action Bar Overflow Menu Customization