如何使布局可用于对话框

时间:2012-08-20 19:20:42

标签: android

在我的活动中有3个按钮。通过单击第一个按钮,我想要一个带有图形的对话框(在布局中它可以正常工作)。

 btn1 = (Button)findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 dialog = new Dialog(ChartsDuration.this);
                 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                 dialog.setContentView(R.layout.dialog_charts1);     

// code to show a graph. Here I have a function that calls  drawChartAll(), 
// but since the layout is declared outside the dialog it cannot render it to the
// linearlayout, and my graph1 linearlayout will be empty.

                 dialog.show();


            }
        });

Tha图使用在

之外的函数中查询的数据
public void drawChartAll()
   {
//blablabla and this is how I define the layout and render the graph to it:
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);   
mChartView = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values),renderer,Type.DEFAULT);
mChartView.setBackgroundColor(renderer.getBackgroundColor());
layout.addView(mChartView);
}

因此,如果没有对话框,我可以轻松地在graph1 LinearLayout中显示图形,例如按钮下方,因为它们“在同一级别”,但我想在单击按钮打开的对话框中显示图形。因为如果我在对话中,我会这样做:LinearLayout layout = (LinearLayout)dialog.findViewById(R.id.graph1);但现在我不能这样做,因为我在对话之外。

如何实现此布局?

修改

user113215我这样做了:

活动中的

 LayoutInflater inflater = LayoutInflater.from(ChartsDuration.this);
 customDialog = (ViewGroup) inflater.inflate(R.layout.dialog_charts1, null);


        btn1 = (Button)findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 dialog.setContentView(customDialog);
                 //queries

                 dialog.show();


            }
        });

并在drawChartAll中:

   public void drawChartAll()
       {
    //code
     LinearLayout layout = (LinearLayout) customDialog.findViewById(R.id.graph1);
    }
这是什么意思?这会向我抛出dialog.setContentView(customDialog);行的空指针异常。

2 个答案:

答案 0 :(得分:1)

如果我正确理解了问题,那么您在进入对话框的布局上操作时会遇到问题。不要调用setContentView(int),而是自己夸大布局,然后使用setContentView(View)

LayoutInflater inflater = LayoutInflater.from(this);
ViewGroup customDialog = (ViewGroup) inflater.inflate(R.layout.dialog_charts1, null);

// Do chart things here
// Prefix all calls to findViewById with "customDialog."
LinearLayout layout = (LinearLayout) customDialog.findViewById(R.id.graph1);
mChartView = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values),renderer,Type.DEFAULT); 
mChartView.setBackgroundColor(renderer.getBackgroundColor()); 
layout.addView(mChartView);

// Put the manipulated layout into the dialog
dialog.setContentView(customDialog);

您可以使用同样的技巧来利用AlertDialog.Builder类,同时仍然使用自定义布局填充对话框。

答案 1 :(得分:0)

dialog变量设置为Activity类中的字段。你可以从任何地方到达它。您仍然可以在整个活动中使用dialog.findviewbyid。在致电setcontentView

之前,请确保已拨打findviewbyid

编辑:我希望你写 since I am outside the dialog. 时意味着你无法访问变量