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

时间:2014-01-30 23:37:55

标签: android xml

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

红线表示默认宽度尺寸。

enter image description here

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

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

布局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 我需要自定义弹出菜单

2 个答案:

答案 0 :(得分:12)

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

步骤1:进行行布局:

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

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

        }

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

            }
        });
    }
  }

希望这会对你有所帮助..如果确实如此,请接受我的答案.. :)

这是截图:

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

答案 1 :(得分:0)

我认为您无法在layout:width中设置menu,因此您可能需要创建自己的视图布局。这可能会有所帮助:

Set menu items centered and full-width with ActionBarSherlock

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