2014-03-31 68 views
0

我嘗試將EdittextListView添加到對話框。 我使用dialog.addContentView(lstcontent,params)方法將視圖添加到對話框。將內容視圖添加到對話框中

這裏是我的代碼:

dialog = new Dialog(MainActivity.this); 

final LinearLayout.LayoutParams lp = new 
LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.WRAP_CONTENT); 

dialog.addContentView(edittext, lp); 
dialog.addContentView(listview,lp); 

dialog.show(); 

,但我的列表視圖是對的EditText。我想在listview上面設置edittext。

here is capture of screen

我從這個太習慣:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -2); 
LinearLayout layout = new LinearLayout(G.context, (AttributeSet) params); 

layout.addView(input); 
layout.addView(lstcontent); 
dialog.setcontentView(layout); 

但這種崩潰的應用程序: 的LayoutParams cannout被轉換爲android.util.Attributeset

+0

你爲什麼不從xml.Simply設置你的活動的主題,對話開始使用intent.It將顯示爲dialog.You可以設置視圖太 –

回答

0

我發現了這個問題的新方法。

LayoutInflater inflate2 = ((Activity) G.context).getLayoutInflater(); 
View dialogView = inflate2.inflate(R.layout.layout_dialog, null); 

input = (EditText) dialogView.findViewById(R.id.edt1); 
lstcontent = (ListView) dialogView.findViewById(R.id.lst1); 

lstcontent.setAdapter(adapter); 

,這是layout_dialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<EditText 
    android:id="@+id/edt1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 
<ListView 
    android:id="@+id/lst1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    >  
</ListView> 

0

它可能是更容易使用dialog.setContentView(R.layout.yourlayout)並使用你自己的佈局

+0

謝謝,但我有一個錯誤,而鑄造paramets –

+0

是什麼錯誤? –

+0

LayoutParams不能投射。再次看到我的問題我編輯了 –

0

你可以使用相對佈局像

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

然後

params.addRule(RelativeLayout.BELOW, id); 

更多信息Programatically add view one below other in relative layout

//編輯

RelativeLayout layout = new RelativeLayout(this); 
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
layout.setLayoutParams(layoutParams); 

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
RelativeLayout.LayoutParams params2 = new 
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

params1.addRule(RelativeLayout.ABOVE, LISTVIEW_ID); 
params2.addRule(RelativeLayout.BELOW, EDITTEXT_INPUT_ID); 

layout.addView(EDITTEXTID, params1); 
layout.addView(LISTVIEW, params2); 

,現在你必須爲你的設置此佈局DIALOG,它應該工作

+0

我加這個,但不工作): –

+0

我編輯的問題...現在試試吧 –