无法在TableRow内的ImageView中动态设置图像

时间:2013-04-06 06:58:53

标签: android camera imageview tablerow

我有这个LinearLayout,在里面,有一个按钮和一个没有行的TableLayout,我将动态添加它们。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn_select" />

<TableLayout
    android:id="@+id/tablay"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</TableLayout>

单击按钮,我希望打开相机并单击图像以在新桌面内的ImageView上设置。 现在这里是java片段。

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button1);
    tableLayout = (TableLayout) findViewById(R.id.tablay);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    bitmap = (Bitmap) data.getExtras().get("data");
    tableRow = new TableRow(this);
    tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    imageView = new ImageView(this);
    imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    imageView.setImageBitmap(bitmap);
    tableRow.addView(imageView);
    tableLayout.addView(tableRow);
}

一切都很顺利,但图像没有显示在屏幕上。

3 个答案:

答案 0 :(得分:0)

修改

tableRow = new TableRow(this);
 imageView = new ImageView(this);

tableRow.addView(imageView,new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT));

像这样设置Params,因为表行只接受TableRow类型的参数

 tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT));

答案 1 :(得分:0)

在main.xml文件中,

    <TableRow
            android:id="@+id/infoRow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                    android:text="Loading Content"
                    android:id="@+id/loadingMessage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
            </TextView>
            <Button
                android:id="@+id/insert"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Add New" />
    </TableRow>

例如:说testItem.java

public class testItem extends Activity
{
    private List<String> results = new ArrayList<String>(); // Please add values to this string list.

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.main);
        int count=results.size();
        TableLayout tl=(TableLayout)findViewById(R.id.mainLayout);
                          for(int i=0;i<count;i++)
                          {
                              final String str=results.get(i);
                              TableRow tr = new TableRow(this);
                              tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                              TextView textview = new TextView(this);
                              textview.setText(str);
                              tr.addView(textview);
                              Button buttn = new Button(this);
                              buttn.setText("DELETE");
                              buttn.setTextColor(Color.BLUE);
                              tr.addView(buttn);
                              tl.addView(tr, new TableLayout.LayoutParams(
                                      LayoutParams.FILL_PARENT,
                                     LayoutParams.WRAP_CONTENT));
                        }

        }
}

答案 2 :(得分:0)

删除imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

更改tableRow.addView(imageView);

tableRow.addView(imageView,100,150);(新的宽度和高度)