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