0

在我的活动中有 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);

4

2 回答 2

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类,同时仍然使用自定义布局填充对话框。

于 2012-08-20T19:25:56.357 回答
0

使dialog变量成为 Activity 类中的字段。你可以从任何地方到达它。您仍然可以dialog.findviewbyid在整个活动中使用。只需确保setcontentView在您致电之前已被致电findviewbyid

编辑:我希望你写since I am outside the dialog.的时候意味着你不能访问变量

于 2012-08-20T19:44:23.807 回答