我在android中仍然是新手。请帮我。我设法将imageview放在布局的左侧和右侧。我的问题是,当我为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/
答案 0 :(得分:0)
将它用于两个imageView。所以你的图像不会重叠
android:adjustViewBounds="true"
android:scaleType="fitXY
答案 1 :(得分: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>
答案 2 :(得分:0)
将您的Imageview make 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
,希望它有效。
答案 3 :(得分:0)
由于所提到的重量,图像会变得拉长。
第一个imageView的android:layout_weight
为1,第二个imageView的{{1}}为2。
这导致第二个imageView占据屏幕的2/3,第一个占第二个。
调整重量,您可以解决问题。