2014-10-09 90 views
0

我正在創建一個對話框並設置佈局的setContentView。而且我正在編程地在對話框setContentView中添加按鈕,圖像到佈局。現在我可以如何將對話框視圖分配給另一個視圖。 這是一個佈局被分配到一個視圖像下面將對話框視圖分配給另一個視圖

View getview=R.layout.tamil_alphabet_speak_word; 

同樣我怎麼能指定的對話框中查看到其他視圖。由於我以編程方式將所有元素添加到視圖「TamilAlphabets」,因此該子元素爲空,它返回下面的代碼。

Alphbetdialog=new Dialog(TamilAlphabets.this); 
    Alphbetdialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    Alphbetdialog.setContentView(R.layout.tamil_alphabetsdialog); 
    (adding elements to the layout "TamilAlphabets" code 
     .............. 
      ) 
    LayoutInflater inflator=(LayoutInflater)TamilAlphabets.this.getSystemService 
    (TamilAlphabets.this.LAYOUT_INFLATER_SERVICE); 
      View row=inflator.inflate(tamil_alphabetsdialog, Parent,false); 
      LinearLayout l1=(LinearLayout)row.findViewById(R.id.alphabetlayout1);       
      ViewGroup vg=(ViewGroup)l1; 
      vg.getChildCount(); 

所以我需要分配對話框視圖到另一個視圖我該怎麼做。 我需要的是這樣的

View getview=<I need dialog box view> 
+0

你想使用自定義對話框嗎? – Saqib 2014-10-09 08:15:03

+0

s我正在使用自定義對話框 – prasad 2014-10-09 08:22:21

+0

如何獲取自定義對話框視圖並將其分配給其他視圖。 – prasad 2014-10-09 08:23:52

回答

0

應避免直接使用Dialog類,而使用Dialog子類的或DialogFragment

https://developer.android.com/guide/topics/ui/dialogs.html

的對話框類是對話的基類,但你應該避免直接實例化Dialog。請改爲使用下列其中一個子類:

AlertDialog 可顯示標題,最多三個按鈕,可選項目列表或自定義佈局的對話框。

DatePickerDialog或TimePickerDialog 帶預定義UI的對話框,允許用戶選擇日期或時間。

在任何情況下即時猜測你想要的是一個自定義對話框,什麼是在這裏這種情況下使用DialogFragment類recomendaded就是一個例子

對話片段佈局

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

對話框類

public class DialogExampleFra gment延伸DialogFragment {

private static final String ARG_PARAM = "extra:PARAM"; 
private String mText; 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Bundle arguments = getArguments(); 

    mText = arguments.getString(ARG_PARAM); 


} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    Dialog dialog = super.onCreateDialog(savedInstanceState); 

    dialog.setTitle("title"); 

    return dialog; 
} 

public static DialogExampleFragment newInstance(String message) { 
    Bundle args = new Bundle(); 

    args.putSerializable(ARG_PARAM, message); 

    DialogExampleFragment fragment = new DialogExampleFragment(); 

    fragment.setArguments(args); 

    return fragment; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View root = inflater.inflate(R.layout.fragment_dialog_example, container, false); 




    TextView t = (TextView) root.findViewById(R.id.textView1); 

    t.setText(mText); 

    return root; 
} 

}

要顯示作爲對話

DialogExampleFragment.newInstance("Message") 
        .show(getFragmentManager(), "dialog"); 

注意由於DialogFragment是它具有能夠解優點被示出爲對話框或作爲片段一個普通的片段,你可以得到我上面發佈的鏈接上的所有信息

相關問題