如何在Android中限制Spinner下拉视图的高度

请建议我用来创建它的任何方法。

查询 :我正在创建2-Spinner视图,我必须添加国家/城市列表,所以如果我选择印度然后我在下拉视图中获得50个项目,问题是它占据了整个页面身高。

我想要的是 :我想创建一个下拉视图,用户在下拉视图中只能看到10个项目,只要用户滚动下拉视图,就会显示其他项目。


我的问题

Solutions Collecting From Web of "如何在Android中限制Spinner下拉视图的高度"

你可以使用Reflection。

Spinner spinner = (Spinner) findViewById(R.id.spinner); try { Field popup = Spinner.class.getDeclaredField("mPopup"); popup.setAccessible(true); // Get private mPopup member variable and try cast to ListPopupWindow android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner); // Set popupWindow height to 500px popupWindow.setHeight(500); } catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) { // silently fail... } 

您还可以通过getWindowVisibleDisplayFrame(Rect outRect) Spinner并覆盖android.widget.PopupWindow用于计算的getWindowVisibleDisplayFrame(Rect outRect)来影响下拉视图的位置和大小。 只需设置outRect即可限制可以显示下拉视图的区域。

这种方法当然不适合所有场景,因为有时你想放置下拉视图,这样它就不会模糊另一个视图或者只是在“实例之外”知道的其他条件。

在我的情况下,我需要将FLAG_LAYOUT_NO_LIMITS标志应用于我的活动窗口,这导致outRect很大,因此下拉视图的一部分有时隐藏在导航栏后面。 为了恢复原始行为,我使用了以下覆盖:

 @Override public void getWindowVisibleDisplayFrame(Rect outRect) { WindowManager wm = (WindowManager) getContext.getSystemService(Context.WINDOW_SERVICE); Display d = wm.getDefaultDisplay(); d.getRectSize(outRect); outRect.set(outRect.left, , outRect.right, outRect.bottom); } 

因为我已经按照@theLittleNaruto的建议在评论部分创建了我自己的PopUpWindow。

main.xml中

 < ?xml version="1.0" encoding="utf-8"?>    

popup_example.xml

 < ?xml version="1.0" encoding="utf-8"?>    

showpopup_1.java

 package com.example.spinnerworking; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageButton; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.PopupWindow.OnDismissListener; import android.widget.Toast; public class showpopup_1 extends Activity { boolean click = true ; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); final LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final Button b = (Button) findViewById(R.id.btn); final View pview = inflater.inflate(R.layout.popup_example, (ViewGroup) findViewById(R.layout.main)); final PopupWindow pw = new PopupWindow(pview); Log.i("hello", "hello") ; b.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (click) { // if onclick written here, it gives null pointer exception. // if onclick is written here it gives runtime exception. pw.showAtLocation(v, Gravity.CENTER_HORIZONTAL, 0, 0); pw.update(8, 0, 150, 200); String[] array = new String[] { "tushar", "pandey", "almora" }; ListView lst = (ListView) pview.findViewById(R.id.lstview); adapterHello adapter = new adapterHello(showpopup_1.this); lst.setAdapter(adapter); lst.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView< ?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(showpopup_1.this, "pandey", Toast.LENGTH_SHORT).show(); } }); click = false ; } else { pw.dismiss(); click = true; } } }); } } class adapterHello extends BaseAdapter { String array[] = new String[] { "tushar", "pandey", "almora", "hello", "tushar", "pandey", "almora", "hello", "tushar", "pandey", "almora", "hello" }; showpopup_1 context; public adapterHello(showpopup_1 context) { this.context = context; } public int getCount() { // TODO Auto-generated method stub return array.length; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub TextView text = new TextView(context); text.setHeight(30); text.setPadding(10, 8, 0, 0); text.setTextColor(Color.BLACK); text.setText(array[position]); text.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("clicked", "tushar"); } }); return text; } } 
  1. android:popupBackground="#00000000"到Spinner中
  2. 在适配器中

 getDropDownView(); parentParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, (int) Utils.convertDpToPx(350)); parentParams.gravity = Gravity.BOTTOM; parent.setLayoutParams(parentParams); 
  1. 你可以通过添加android:dropDownVerticalOffset="60dp"来移动popup窗口android:dropDownVerticalOffset="60dp"