2015-02-24 52 views
-1

我得到元素的標記和ID,但無法設置可見性或onClickListener。你能告訴我爲什麼嗎?代碼: onClickListener和知名度,這應該使視圖不可見,在clicklistener應該說DS(我第一次嘗試的一個然後其他):無法在PopUp窗口中的視圖上設置onClickListener:

ViewGroup layoutHolder = (ViewGroup) inflater.inflate(context.getResources().getIdentifier("settings", 
           "layout", context.getPackageName()), (ViewGroup) this.findViewById(getResources().getIdentifier("settings", 
         "id", getPackageName())), false); 
layoutHolder.findViewById(R.id.msettings).setOnClickListener(handler) 
View.OnClickListener handler = new View.OnClickListener() { 
     public void onClick(View v) { 
     Toast.makeText(getApplicationContext(), "ds", Toast.LENGTH_SHORT).show(); 
    }; 

彈出:

LayoutInflater layoutInflater 
        = (LayoutInflater)getBaseContext() 
        .getSystemService(LAYOUT_INFLATER_SERVICE); 
      View popupView = layoutInflater.inflate(R.layout.settings, null); 
      final PopupWindow popupWindow = new PopupWindow(
        popupView, 
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 
      Bitmap bitmap = null; 
      popupWindow.setBackgroundDrawable(new BitmapDrawable(getApplicationContext().getResources(), bitmap)); 
      popupWindow.setOutsideTouchable(true); 
      popupWindow.showAsDropDown(v, 50, 11); 

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:id="@+id/settings" 
    android:tag="settings" 
    android:background="@drawable/shadow_light" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:paddingTop="5dp"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:orientation="horizontal" 
     android:gravity="center_horizontal|center_vertical" 
     android:background="@drawable/fast_menu_background" 
     android:id="@+id/top_menu" 
     android:tag="menu_top"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:id="@+id/top_menu_sub" 
      android:tag="menu_top"> 
      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:src="@drawable/fast_menu_map_settings" 
       android:background="#00000000" 
       android:adjustViewBounds="true" 
       android:scaleType="fitXY" 
       android:layout_marginLeft="5dp" 
       android:layout_marginStart="5dp" 
       android:layout_weight="1" 
       android:contentDescription="@string/desc_st" 
       android:id="@+id/msettings" 
       android:tag="msettings"/> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 
+1

顯示代碼第一 – 2015-02-24 12:36:40

+0

我已經添加了代碼 – user4597015 2015-02-24 12:40:09

+0

顯示辣油,並在您試圖上點擊收聽 – 2015-02-24 12:40:55

回答

0

請嘗試像下面的代碼。

LayoutInflater layoutInflater 
        = (LayoutInflater)getBaseContext() 
        .getSystemService(LAYOUT_INFLATER_SERVICE); 
      View popupView = layoutInflater.inflate(R.layout.settings, null); 

popupView.findViewById(R.id.msettings).setOnClickListener(handler) 
View.OnClickListener handler = new View.OnClickListener() { 
     public void onClick(View v) { 
     Toast.makeText(getApplicationContext(), "ds", Toast.LENGTH_SHORT).show(); 
    }; 
final PopupWindow popupWindow = new PopupWindow(
        popupView, 
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 
      Bitmap bitmap = null; 
      popupWindow.setBackgroundDrawable(new BitmapDrawable(getApplicationContext().getResources(), bitmap)); 
      popupWindow.setOutsideTouchable(true); 
      popupWindow.showAsDropDown(v, 50, 11); 
+0

如果有用接受。 – 2015-02-24 12:57:26

+0

結果是一樣的,onclicklistener仍未設置。 – user4597015 2015-02-24 13:00:34