2013-12-15 44 views
14

請提出任何我用來創建它的方法。如何限制Android中的微調器下拉視圖的高度

查詢:我創建了2-微調視圖,在這裏我要補充國家/城市名單,所以像如果我選擇印度,然後我得到50項下拉視圖中,問題這是它正在整個頁面的高度。

我想什麼:我想創建一個下拉視圖,在那裏用戶可以看到在 下拉觀止10個項目,其他項目將顯示用戶只要將滾動下拉視圖。


My problem

+2

您應該使用自定義PopupWindow。 – TheLittleNaruto

+0

謝謝,@TheLittleNaruto,它的工作原理,並解決了我的問題。 –

+0

@TusharPandey:您好Tushar,我面臨同樣的問題,但您接受的答案不適用於API級別5.0 ...您有任何解決方案嗎? –

回答

37

您移動彈出可以使用反射。

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... 
    } 
+2

也請解釋源代碼的重要部分,以改進您的答案並使其更有用。 – foobar

+0

它拋出異常java.lang.ClassCastException:android.widget.Spinner $ DropdownPopup不能轉換爲android.support.v7.widget.ListPopupWindow –

+0

爲我工作沒有任何問題。另請注意,您可以使用MATCH_PARENT或WRAP_CONTENT調用setHeight(int)方法(如ListPopupWindow.setHeight(int)方法Javadoc中所述)。 – dazed

3

爲我創造了我自己的,PopUpWindow通過@theLittleNaruto的建議,在評論部分。

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <Button 
     android:layout_marginTop="80dp" 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Country" 
     android:layout_gravity="center_vertical|center_horizontal"/> 
</LinearLayout> 

popup_example.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="10dip" > 

    <ListView 
     android:id="@+id/lstview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

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

} 
0
  1. 在適配器

getDropDownView(); 
parentParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, (int) Utils.convertDpToPx(350)); 
parentParams.gravity = Gravity.BOTTOM; 
parent.setLayoutParams(parentParams); 
添加 android:popupBackground="#00000000"到微調
  • 可以通過添加android:dropDownVerticalOffset="60dp"
  • +0

    什麼是「getDropDownView」,什麼是「父母」? –

    7

    您也可以影響通過繼承Spinner,並覆蓋其getWindowVisibleDisplayFrame(Rect outRect)這是計算中使用的android.widget.PopupWindow下拉視圖的位置和大小。只需設置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, <STATUS BAR HEIGHT>, outRect.right, outRect.bottom); 
    } 
    
    +0

    你解決了我的問題夥計。我還必須使狀態欄完全透明 – Thecarisma