【问题标题】:Expand Layout and insert some fields dynamically展开布局并动态插入一些字段
【发布时间】:2012-12-21 11:04:35
【问题描述】:

我想开发一个应用程序,其中屏幕上有一个按钮,单击该按钮后,相机将打开,然后拍照,按钮所在的布局展开,照片位于此下方按钮。

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

点击照片后,它会在此处拍摄照片,然后此布局展开,照片如下所示:-

我想在我的应用程序中使用同样的东西。

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

(我的应用程序中已经有一些字段在 TableLayout 中的 TableRow 中)

首先我创建一个 tableRow1(比如说),然后创建一个 TableLayout,然后为按钮创建一个 TableRow2,为 Image 创建一个 TableRow3。我将按钮插入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));
}

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

【问题讨论】:

    标签: android android-layout android-tablelayout


    【解决方案1】:

    尝试对添加到TableRows 的视图也使用正确的LayoutParams

    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,然后将tableRow1 添加(如果它不在布局中)到主表。在这种情况下:

    tableRow1.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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 2015-03-21
      • 2021-05-31
      • 2012-05-17
      • 1970-01-01
      • 2019-07-20
      • 2017-12-13
      相关资源
      最近更新 更多