2

我有这个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);
}

一切正常,但图像没有显示在屏幕上。

4

3 回答 3

0

编辑:

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

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

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

 tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT));
于 2013-04-06T07:05:32.103 回答
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));
                        }

        }
}
于 2013-04-06T07:20:02.870 回答
0

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

改变tableRow.addView(imageView);

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

于 2018-04-11T13:11:19.727 回答