我在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;
}
}