2012-08-27 70 views
4

我心裏有一個想法,但林不知道如何實現它如何佈局添加到對話

首先我有一個對話框

final Dialog dialog = new Dialog(mContext); 

我也有一個佈局

<?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="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textViewTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center_horizontal" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textViewDescription" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center_horizontal" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/textViewWhen" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center_horizontal" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

我想要的是在對話框中添加此佈局,我可能還想在該對話框內添加更多相同的佈局 我該怎麼做?

例如,如何在一個對話框中添加兩個這種佈局? 像


Dialog Title 

    Large Text 
    Small Text 
    Medium Text 


    Large Text 
    Small Text 
    Medium Text 

回答

3

這是從我的應用的示例:

public class ConfirmDialog extends DialogFragment { 

public static String TAG = "Confirm Dialog"; 

public interface ConfirmDialogCompliant { 
    public void doOkConfirmClick(); 
    public void doCancelConfirmClick(); 
} 

private ConfirmDialogCompliant caller; 
private String message; 

public ConfirmDialog(ConfirmDialogCompliant caller, String message){ 
    super(); 
    this.caller = caller; 
    this.message = message; 
} 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    final View view = inflater.inflate(R.layout.confirm_dialog, container, false); 
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 
    ((TextView) view.findViewById(R.id.textview_confirm)).setText(message); 
    ((Button) view.findViewById(R.id.ok_confirm_button)).setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // When button is clicked, call up to owning activity. 
      caller.doOkConfirmClick(); 
     } 
    }); 
    ((Button) view.findViewById(R.id.cancel_confirm_button)).setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // When button is clicked, call up to owning activity. 
      caller.doCancelConfirmClick(); 
     } 
    }); 
    return view; 
} 

} 

其中充氣佈局confirm_dialog.xml。
您在onCreateView方法中膨脹佈局。
在這種情況下,我使用了DialogFragment(我建議您使用...請參閱支持庫,以便您不必擔心目標SDK),但同樣適用於Dialog。
希望它可以幫助你!

6

事情是這樣的:

LayoutInflater li = LayoutInflater.from(SomeActivity.this); 
someLayout = (LinearLayout)li.inflate(R.layout.some_layout, null); 

alert = new AlertDialog.Builder(SettingsActivity.this); 
alert.setView(someLayout);