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

How can I change popup item width size?(如何更改弹出项目宽度大小?)

本文介绍了如何更改弹出项目宽度大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use popup menu with custom items.Problems like this

红线代表默认宽度大小.

Red lines represents to default width size.

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

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

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

布局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 文件

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

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

Similar question: stackoverflow.com I need to custom popup menu

推荐答案

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

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:制作行布局:

Step1: Making a row layout:

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

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;

        }

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

Step 3: Simply calling it in the 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..:)

截图如下:

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

这篇关于如何更改弹出项目宽度大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何更改弹出项目宽度大小?

基础教程推荐