展开布局并动态插入一些字段

时间:2012-12-21 11:04:36

标签: android android-layout android-tablelayout

我想开发一个应用程序,其中屏幕上会有一个按钮,点击该按钮后相机将打开,之后拍照,按钮在那里的布局展开,照片在下面按钮。

我在一个屏幕截图的应用程序中显示这个东西:

enter image description here

点击照片后,它会捕捉照片,然后展开这个布局,照片就像下面这样: -

enter image description here enter image description here

我在申请中要同样的事情。

为此我尝试了一些代码: -

(TableRay中的TableRow中的某些字段已存在于TableLay中)

首先我创建一个tableRow1(比如说)之后我创建了一个TableLayout然后一个TableRow2用于按钮,一个TableRow3用于Image。我将按钮插入tableRow2,然后将此tableRow2插入TableLayout,然后将TableLayout插入tableRow1。拍摄照片后,我将Image插入TableRow3,然后将此tableRow3插入TableLayout。

但是这段代码不起作用,这段代码也没有给出任何错误。

代码:

tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

tableLayout = new TableLayout(this);
tableRow2 = new TableRow(this);

tableLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tableRow2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

buttonPhoto = new Button(this);
buttonPhoto.setText("Photo");
buttonPhoto.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

tableRow2.addView(buttonPhoto);

tableLayout.addView(tableRow2, new TableLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

tableLayoutMain.addView(tableLayout, new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));



buttonPhoto.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            method1();
        }
    });

public void method1(){

    tableRow3 = new TableRow(this);

    tableRow3.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    imageViewPhoto = new ImageView(this);
    imageViewPhoto.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 110));
    tableRow3.addView(imageViewTakePhoto);

    tableLayout.addView(tableRow3, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}

通过使用此代码,我甚至无法在屏幕上看到按钮。

1 个答案:

答案 0 :(得分:0)

尝试使用适当的LayoutParams来添加到TableRows的视图:

tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
tableLayout = new TableLayout(this);
tableRow2 = new TableRow(this);
buttonPhoto = new Button(this);
buttonPhoto.setText("Photo");
buttonPhoto.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tableRow2.addView(buttonLocationPhoto);
tableLayout.addView(tableRow2, new TableLayout.LayoutParams(
            TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
buttonPhoto.setOnClickListener //...

和方法:

public void method1(){
    tableRow3 = new TableRow(this);
    imageViewPhoto = new ImageView(this);
    imageViewPhoto.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 110));
    tableRow3.addView(imageViewTakePhoto);
    tableLayout.addView(tableRow3, new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
}

此外,在您的问题中,我看到您说的是tabLelayout添加到tableRow1,然后添加(如果它尚未在布局中)tableRow1tableRow1.addView(tableLayout, new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); //add the tableRow1 to the main table?!? 主表。在这种情况下:

tableLayoutMain.addView(tableLayout, new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

但是,在你的代码中你没有这个,而是你有这一行:

{{1}}