Android 如何更改弹出项目的宽度大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21469792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:37:36  来源:igfitidea点击:

How can I change popup item width size?

androidxml

提问by Asil ARSLAN

I use popup menu with custom items.Problems like this

我使用带有自定义项目的弹出菜单。像这样的问题

Red lines represents to default width size.

红线代表默认宽度大小。

enter image description here

在此处输入图片说明

but i want custom smaller width size of items(represents red lines).

但我想要自定义较小宽度的项目(代表红线)。

enter image description here

在此处输入图片说明

     public void onClick(View v) {  
      //Creating the instance of PopupMenu  
      PopupMenu popup = new PopupMenu(MainLayout.this, mSikBtn);  
      popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
      popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
       public boolean onMenuItemClick(MenuItem item) {  
        ...

        return true;  
       }  
      });  

      popup.show();//showing popup menu  
     }  
    });

Layout xml file

布局xml文件

<ImageView
        android:id="@+id/popupBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent" 
        android:src="@drawable/selector_sik"
        android:layout_weight="1"
        android:layout_margin="10dip"
        />

popup_menu.xml file

popup_menu.xml 文件

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item  
        android:id="@+id/one"  
        android:title="A)."/>  
      ...

    <item  
        android:id="@+id/three"  
        android:title="E)."/>  

</menu>

Similar question: stackoverflow.comI need to custom popup menu

类似问题:stackoverflow.com我需要自定义弹出菜单

回答by mike20132013

Just like you, I tried a lot to change the width of the popup menu but was unsuccessful. Somehow, I found a solution you might be looking for. First, instead of using popup menu, I used popup window.

就像你一样,我尝试了很多来改变弹出菜单的宽度,但没有成功。不知何故,我找到了您可能正在寻找的解决方案。首先,我没有使用弹出菜单,而是使用弹出窗口。

Step1: Making a row layout:

Step1:制作行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@android:drawable/list_selector_background"
    android:orientation="vertical" 
    android:padding="3dp">

    <TextView
        android:id="@+id/ItemA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="3dp"
        android:text="A)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/ItemB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="10dp"
        android:text="B)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        tools:ignore="HardcodedText" />
    <TextView
        android:id="@+id/ItemC"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="10dp"
        android:text="C)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        tools:ignore="HardcodedText" />
    <TextView
        android:id="@+id/ItemD"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="10dp"
        android:text="D)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        tools:ignore="HardcodedText" />

</LinearLayout>

Step 2: Made a method to initialize popup window:

第 2 步:创建一个初始化弹出窗口的方法:

private PopupWindow initiatePopupWindow() {

            try { 

                mInflater = (LayoutInflater) getApplicationContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View layout = mInflater.inflate(R.layout.row, null);

                        //If you want to add any listeners to your textviews, these are two //textviews.
                final TextView itema = (TextView) layout.findViewById(R.id.ItemA);


                final TextView itemb = (TextView) layout.findViewById(R.id.ItemB);



                layout.measure(View.MeasureSpec.UNSPECIFIED,
                        View.MeasureSpec.UNSPECIFIED);
                mDropdown = new PopupWindow(layout,FrameLayout.LayoutParams.WRAP_CONTENT,
                        FrameLayout.LayoutParams.WRAP_CONTENT,true);
                Drawable background = getResources().getDrawable(android.R.drawable.editbox_dropdown_dark_frame);
                mDropdown.setBackgroundDrawable(background);
                mDropdown.showAsDropDown(pop, 5, 5);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return mDropdown;

        }

Step 3: Simply calling it in the onCreate():

第 3 步:只需在 onCreate() 中调用它:

public class MainActivity extends Activity {

    ImageButton red, blue;

    private PopupWindow mDropdown = null;
    LayoutInflater mInflater;
    Button pop;

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

        pop = (Button)findViewById(R.id.button1);
        pop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                initiatePopupWindow();

            }
        });
    }
  }

Hope this helps you..If it does, accept my answer..:)

希望这对您有帮助..如果有帮助,请接受我的回答..:)

Here's the screenshot:

这是屏幕截图:

http://imageshack.com/a/img585/7388/dxjo.png

http://imageshack.com/a/img585/7388/dxjo.png

回答by SQLiteNoob

I don't think you can set layout:widthin menu, so you might have to create your own view layout. This might help:

我认为您不能设置layout:widthin menu,因此您可能必须创建自己的视图布局。这可能有帮助:

Set menu items centered and full-width with ActionBarSherlock

使用 ActionBarSherlock 设置居中和全宽的菜单项

http://developer.android.com/guide/topics/resources/menu-resource.html

http://developer.android.com/guide/topics/resources/menu-resource.html