如何以编程方式添加imageview

时间:2014-05-03 05:31:24

标签: android android-layout imageview android-linearlayout

我正在以编程方式将imageview添加到布局

image_layout.addView(imSex,imParams);

这里有一个图像声明 oncreate

ImageView imSex ;
LinearLayout image_layout;
LinearLayout.LayoutParams  imParams;

在oncreate之后

image_layout=(LinearLayout)findViewById(R.id.image_layout);
imParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
imSex= new ImageView(Detail_mul.this);
imSex.setBackgroundResource(R.drawable.camera);
image_layout.setGravity(Gravity.CENTER); 

这是xml

how to

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/Top_layout"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@drawable/title_bar_bg" >

    <ImageView
        android:id="@+id/app_name"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/logo" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/layout_sub_middle"
    android:layout_width="match_parent"
    android:layout_height="38dp"
    android:background="#D4D5D5" >

    <TextView
        android:id="@+id/textView_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Fyll ut skjemaet"
        android:textSize="25dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#000"
        android:gravity="bottom"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/addpic_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:text="Velg hoved bilde" />

            <LinearLayout
                android:id="@+id/image_layout"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:gravity="center" >

                <ImageView
                    android:id="@+id/im1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/camera" />
            </LinearLayout>

            <Button
                android:id="@+id/submit"
                android:layout_width="fill_parent"
                android:layout_height="45dp"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp"
                android:background="@drawable/login_btn"
                android:text="@string/submit"
                android:textColor="#FFF"
                android:textSize="20dp" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

</LinearLayout>

以下是我添加imageview的方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            try {
                Bitmap bm;
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
                bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions);
                bm = Bitmap.createScaledBitmap(bm, 300, 200, true);
                im1.setImageBitmap(bm);
                String path = android.os.Environment.getExternalStorageDirectory()+ File.separator+ "Phoenix" + File.separator + "default";
                PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("my_image", f.toString()).commit();
                //f.delete();
                OutputStream fOut = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                try {
                    fOut = new FileOutputStream(file);
                    bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == SELECT_FILE) 
        {
            Uri selectedImageUri = data.getData();
            String tempPath = getPath(selectedImageUri, Detail_mul.this);
            PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().putString("my_image", tempPath).commit();
            Bitmap bm;
            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bm = BitmapFactory.decodeFile(tempPath,btmapOptions);
            bm = Bitmap.createScaledBitmap(bm, 300, 200, true);

            bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
            im1.setImageBitmap(bm);
        }
    }

    image_layout.addView(imSex,imParams); //this line works fine
}

添加imageview调试很好但视觉上没有在视图上添加imageview。

1 个答案:

答案 0 :(得分:0)

通过在xml中添加android:orientation="vertical"来正常工作