2015-07-01 45 views
-2

我想創建在自定義對話框內動態創建的表格佈局。誰能告訴我如何做到這一點?如何使用動態表格佈局來拋棄自定義對話框?

我做了一些事情,但沒有在對話框中顯示。該對話框中沒有表格佈局。

有誰能告訴我我犯了什麼錯誤嗎? 這是我的活動:

LayoutInflater inflater = getActivity().getLayoutInflater(); 
View dialog_view = inflater.inflate(R.layout.progressdialog, null); 
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity()); 
dialog.setView(dialog_view); 
TableLayout table_layout = (TableLayout)dialog_view.findViewById(R.id.Table_view_in_dialog); 
for (int i = 1; i <= 4; i++) { 
    TableRow row = new TableRow(getActivity()); 
    row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
    LayoutParams.WRAP_CONTENT)); 

    // inner for loop 
    for (int j = 1; j <= 4; j++) { 
     TextView tv = new TextView(getActivity()); 
     tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
           LayoutParams.WRAP_CONTENT)); 
         tv.setBackgroundResource(R.drawable.cell_shape); 
     tv.setPadding(5, 5, 5, 5); 
     tv.setText("R " + i + ", C" + j); 
     row.addView(tv); 

    } 

    table_layout.addView(row); 

} 
AlertDialog alert = dialog.create(); 
alert.show(); 

回答

0

你爲什麼不使用對話片段,http://developer.android.com/reference/android/app/DialogFragment.html

這是最好的使用,你可以管理這個樣片段,因爲這有自己的UI

public class SearchWeeklyAlertDialog extends DialogFragment 
{ 
@Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
    } 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NewDialog); 
    } 

@Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.dialog_serach_days, container); 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

// your stuff like find ids and perform operations 
} 

} 

,你可以調用這個作爲

SearchWeeklyAlertDialog dialog = new SearchWeeklyAlertDialog(); 
    dialog.show(getChildFragmentManager(), DIALOG_FRAGMENT); 
相關問題