0

我在android中还是新手。请帮我。我设法将图像视图放在布局的左侧和右侧。我的问题是,当我为 imageview2 选择图像时,它会将图像传递给 imageview1,但仍然可以从右侧看到 imageview2。

我需要做的是,当我为 imageview2 选择图像时,它应该固定在右侧。我认为我在 java 中的代码有问题?

这是我的xml代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
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" >

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/insert_bg"
    >

    <LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip"
>

 <ImageView
     android:id="@+id/img_left"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:fitsSystemWindows="false"
     android:scaleType="fitStart"
     android:src="@drawable/insert_ci" />

 <ImageView
     android:id="@+id/img_center"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="2"
     android:fitsSystemWindows="false"
     android:scaleType="fitEnd"
     android:src="@drawable/insert_ci"
     android:textAlignment="viewEnd" />

 </LinearLayout>

而这个java代码。

package com.prototype;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.ImageButton;
import android.widget.ImageView;
//import android.widget.LinearLayout;

public class MainActivity3 extends Activity {

private static final int SELECT_PICTURE = 2;

private String selectedImagePath;
private String selectedImagePath2;
private ImageView img;
private ImageView img2;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.open_project);

    img = (ImageView)findViewById(R.id.img_left);
    img2= (ImageView)findViewById(R.id.img_center);

    addImageViewClickListener();
    addImageView2ClickListener();
}
public void addImageViewClickListener()
{
ImageView btnNavigator1 = (ImageView)findViewById(R.id.img_left);
btnNavigator1.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View arg0) 

            {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select character image for right side."), SELECT_PICTURE);
            }

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

            //}
        });
}





public void addImageView2ClickListener()
{
ImageView btnNavigator2 = (ImageView)findViewById(R.id.img_center);
btnNavigator2.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View arg0) 

            {

                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select character image for left side."), SELECT_PICTURE);
            }

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

            //}
        });
}



public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) 
    {
        if (requestCode == SELECT_PICTURE) 
        {
            //File folder = new File(Environment.getExternalStorageDirectory() + "/Database/");
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            img.setImageURI(selectedImageUri);
        }
    }
}

public void onActivityResult2(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) 
    {
        if (requestCode == SELECT_PICTURE) 
        {
            //File folder = new File(Environment.getExternalStorageDirectory() + "/Database/");
            Uri selectedImageUri = data.getData();
            selectedImagePath2 = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath2);
            img2.setImageURI(selectedImageUri);
        }
    }
}


public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);

}

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

我说的问题是这样的。http://imageshack.us/photo/my-images/31/6sa6.png/

打印屏幕图像

4

4 回答 4

0

The image is getting stretched because of the weight mentioned.

The android:layout_weight for the first imageView is 1 and that is for second imageView is 2.

This leads to taking 2/3rd of the screen by the the second imageView and the rest by the first.

Adjust the weights and you can overcome the issue.

于 2013-09-26T05:43:46.823 回答
0

将此用于两个图像视图。所以你的图像不会重叠

        android:adjustViewBounds="true"
                android:scaleType="fitXY
于 2013-09-26T05:02:42.693 回答
0

试试这个,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/img_left"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:fitsSystemWindows="false"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/img_center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:fitsSystemWindows="false"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher"
    android:textAlignment="viewEnd" />

 </LinearLayout>
于 2013-09-26T05:03:36.260 回答
0

将您的 Imageview 更改0dp为您的 android:layout_width@

 <ImageView
     android:id="@+id/img_left"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:fitsSystemWindows="false"
     android:scaleType="fitStart"
     android:src="@drawable/icon" />

 <ImageView
     android:id="@+id/img_center"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_weight="2"
     android:fitsSystemWindows="false"
     android:scaleType="fitEnd"
     android:src="@drawable/icon"
     android:textAlignment="viewEnd" />

更新

再创建一个名为的文件夹layout-land,并xml在里面进行适当的更改,希望它可以工作。

于 2013-09-26T05:09:12.090 回答