2014-07-22 127 views
1

我已經通過膨脹以下XML創建AlertDialog:無法查看按鈕(一個或多個)在充氣視圖

LayoutInflater li = LayoutInflater.from(this); 
    View dialogView = li.inflate(R.layout.activity_list_logs, null); 
    ListView list = (ListView) dialogView.findViewById(R.id.listDates); 

用於示出alertDialog完整的代碼

private void showLogs(final List<Absentees> abs) { 
    LayoutInflater li = LayoutInflater.from(this); 
    View dialogView = li.inflate(R.layout.activity_list_logs, null); 
    ListView list = (ListView) dialogView.findViewById(R.id.listDates); 
    list.setAdapter(new CustomAdapterTagAbsentees(this,abs)); 
    list.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapter, View view,int position, long id) { 
       Absentees a =(Absentees)adapter.getItemAtPosition(position); 
       try 
       { 
        showLogDetails(a); 
       } 
       catch(Exception e) 
       { 
        show_popup(e+""); 
       } 
      } 

     }); 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
    alertDialogBuilder.setView(dialogView); 
    alertDialogBuilder.setTitle("Attendance Details:"); 
    alertDialogBuilder.setMessage("Day : "); 
    alertDialogBuilder 
     .setCancelable(true) 
     .setPositiveButton("Dismiss", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int id) { 
       dialog.cancel(); 
      } 
     }); 
    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 
} 

的使用的佈局:

<?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:background="@color/white" 
> 
<ListView 
    android:id="@+id/listDates" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:textSize="20sp"  
    /> 
</LinearLayout> 

關於我的問題:

如果列表足夠小以適應屏幕我可以看到「關閉」按鈕(positiveButton)。 See the Normal Image

否則,如果列表太大,我無法看到「辭退」按鈕

Please see the bottom side!!

請幫助我!,我嘗試將按鈕添加到的內底佈局本身,它的顯示正確,但我不知道爲什麼這個默認的顯示positiveButton的方式完全不

+1

'AlertDialog.Builder'早已內部ListView和所述setAdapter方法。只需刪除所有的視圖充氣的東西,並調用'alertDialogBu​​ilder.setAdapter(new,CustomAdapterTagAbsentees(this,abs)' – Blackbelt

+0

@blackbelt這是否適用於自定義適配器? – Shan

+0

當然它確實是 – Blackbelt

回答

1

顯示使用RelativeLayout的,而不是線性。將按鈕設置爲layout_alignParentBottom =「true」。把列表視圖layout_above =「按鈕的ID」。這將強制按鈕到屏幕的底部,並在繪製列表視圖之前爲它們預留空間。否則,listview是貪婪的,會吸收所有需要的空間。

+0

我認爲將按鈕放置到列表的頁腳也會起作用。 –

+0

@AlexanderMalakhov是的,可能。你使用的是個人喜好。我喜歡完全不在列表中,因爲它處理的是單獨的問題,但它會得到他想要的效果。 –

+0

@AlexanderMalakhov是我用盡它已經,什麼IM問的是:爲什麼按鈕沒有顯示,如果設置了由setPositveButton法,該按鈕! – Shan

0

這裏是我的問題的最終解決方案,感謝@blackbelt您的建議

private void showLogs(final List<Absentees> abs) { 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
    final ListAdapter adapter = new CustomAdapterTagAbsentees(this,abs); 
    alertDialogBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      showLogs(abs); 
      Absentees a =(Absentees) adapter.getItem(item); 
      showLogDetails(a); 
     } 
    }); 

    alertDialogBuilder.setTitle("Attendance Details:"); 
    //alertDialogBuilder.setMessage("Day : XXXX"); 
    alertDialogBuilder 
     .setCancelable(false) 
     .setPositiveButton("Dismiss", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int id) { 
       dialog.cancel(); 
      } 
     }); 
    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 
} 

我要評論/刪除

alertDialogBuilder.setMessage("Day : XXXX "); 

爲succesfull執行,不知道爲什麼!