2016-02-26 72 views
0

我正在查找如何在列表視圖標題中單擊按鈕時顯示菜單。 菜單有按鈕,複選框,微調框和文本視圖。 然後我不能使用彈出菜單。我猜。我試圖用一個彈出窗口來做到這一點,但是當我打開彈出窗口時,如果滾動窗口,不要保持按鈕的錨點。 我想要的是在列表視圖項目上顯示菜單並將其固定到按鈕的底部,當我滾動列表視圖時,菜單仍然錨定在按鈕上。 主要問題是彈出式窗口會覆蓋當前的活動,而不是在 任何想法?列表視圖標題中的菜單

非常感謝!

+1

請發表一些代碼,以改善您的問題 –

回答

0

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private ArrayAdapter<String> adapter; 

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

     final ListView mDrawerList = (ListView) findViewById(R.id.lw1); 
     final View header = getLayoutInflater().inflate(R.layout.header_lw, mDrawerList, false); 
     mDrawerList.addHeaderView(header, null, false); 

     ArrayList<String> itemss = new ArrayList<String>(); 
     itemss.add("Title1"); 
     itemss.add("Title2"); 
     itemss.add("Title3"); 
     itemss.add("Title4"); 
     itemss.add("Title5"); 
     itemss.add("Title6"); 
     itemss.add("Title7"); 
     itemss.add("Title8"); 
     itemss.add("Title9"); 
     itemss.add("Title10"); 
     itemss.add("Title11"); 
     itemss.add("Title12"); 
     itemss.add("Title13"); 
     itemss.add("Title14"); 
     itemss.add("Title15"); 
     itemss.add("Title16"); 
     itemss.add("Title17"); 
     itemss.add("Title18"); 
     itemss.add("Title19"); 
     itemss.add("Title20"); 
     itemss.add("Title21"); 
     itemss.add("Title22"); 

     adapter=new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, 
       itemss); 
     mDrawerList.setAdapter(adapter); 




     final Button btn1 = (Button)findViewById(R.id.button); 
     btn1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       LayoutInflater layoutInflater = (LayoutInflater) mDrawerList.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View popupView = layoutInflater.inflate(R.layout.option_menu, null); 
      final PopupWindow popupWindow = new PopupWindow(
       popupView, 
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 

       popupWindow.showAsDropDown(btn1); 


      } 
     }); 

    } 
} 

activity_main.xml中

<?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="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".15"></RelativeLayout> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:id="@+id/lw1" 
     android:choiceMode="none" 
     android:focusable="false" 
     android:headerDividersEnabled="false" 
     android:layout_weight=".85" 
     android:divider="@null" 
     android:dividerHeight="0dp" 
     android:listSelector="#00000000" 
     android:cacheColorHint="#00000000" /> 

</LinearLayout> 

option_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1" 
    android:id="@+id/lnOption" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:paddingTop="15dp"> 

     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Cb1" 
      android:id="@+id/cbAllOff" 
      android:checked="false" 
      android:buttonTintMode="src_atop"/> 

     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Cb2" 
      android:id="@+id/cbAllSpecOff" 
      android:layout_below="@+id/cbAllOff" 
      android:checked="false" 
      android:buttonTintMode="src_atop"/> 

    </RelativeLayout> 

</LinearLayout> 

header_lw.xml

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

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Menu" 
     android:id="@+id/button" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

lw_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="1"> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".4"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Medium Text" 
      android:id="@+id/txt1" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".6"></RelativeLayout> 
</LinearLayout> 

option_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1" 
    android:id="@+id/lnOption" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:paddingTop="15dp"> 

     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Cb1" 
      android:id="@+id/cbAllOff" 
      android:checked="false" 
      android:buttonTintMode="src_atop"/> 

     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Cb2" 
      android:id="@+id/cbAllSpecOff" 
      android:layout_below="@+id/cbAllOff" 
      android:checked="false" 
      android:buttonTintMode="src_atop"/> 

    </RelativeLayout> 

</LinearLayout> 

當我點擊按鈕固定在按鈕的彈出窗口顯示。如果我滾動列表視圖並且標題中的按鈕變得不可見,則彈出窗口會在操作欄上顯示。有一種方法可以在列表項上顯示菜單選項並將其固定到標題中的按鈕上?

謝謝!