我有这个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);
}
一切正常,但图像没有显示在屏幕上。