0

我還是新來的這裏在Android。請幫幫我。我設法將imageview放在我的Layout的左側和右側。我的問題是,當我爲imageview2選擇圖像時,它會將圖像傳遞給imageview1,並且仍然可以從右側看到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/

printscreen image

回答

0

使用此兩個imageViews。所以,你的圖像將不會重疊

 android:adjustViewBounds="true" 
       android:scaleType="fitXY 
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> 
+0

它仍然是這樣的。當我爲imageview2選擇圖像時,它傳遞給imageview1或類似的東西。 – Suprance

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" /> 

更新

創建多一個名爲的文件夾並保持相同的xml內適當的變化,希望它的作品。

+0

它仍然是這樣的。我加載imageview2的圖像有點或我不知道傳遞給imageview1我不知道我的問題是在xml還是在java中。 – Suprance

+0

無法訪問圖片鏈接,你可以發佈你的問題本身嗎? – RobinHood

+0

我用你的代碼和定位是好的,但是當我爲imageview2選擇一個圖像時,我仍然可以看到imageview2,並且所選圖像在imageview1中輸入。對不起,如果我沒有解釋清楚。我遇到了我的語法問題 – Suprance

0

由於提到的重量,圖像變得拉伸。

android:layout_weight用於第一ImageView的是1,並且爲第二ImageView的是2

這導致服用屏幕2 /第三由第二ImageView的並且由第一其餘部分。

調整重量並解決問題。

+0

我試着將它調整爲1和.5,但結果仍然存在。當我爲imageview2選擇圖像時,我需要在右側的圖像位於左側,而imageview2仍然可以看到。 我認爲圖像傳遞到imageview1 – Suprance

+0

你可以刪除重量,並嘗試 – Anu

+0

問題仍然存在。我選擇的圖像輸入imageview1 http://postimg.org/image/iawr6mmn1/ – Suprance