8

저는 ImageView이며 layout_height을 "100dp"로 설정하고 layout_width를 "wrap_content"로 설정했습니다.ImageView adjustViewBounds가 상대 레이아웃과 작동하지 않습니다.

사용 가능한 드로어 블은 130dp (너비) X 215dp (높이)보다 큰 실제 크기를 사용합니다. I adjustViewBounds true로 설정하는 경우가 ImageView하는 LinearLayout 내에 배치된다

, 상기 그리기의 종횡비와 layout_width에 따라 측정되는 폭을 얻는다.

그러나 RelativeLayout 안에 배치하면 ImageView은 adjustViewBounds 설정에 관계없이 드로어 블 (130dp)과 동일한 너비를 갖습니다.

두 경우 모두 이미지가 왜곡없이 렌더링되지만 이미지 뷰의 경계는 다릅니다.

RelativeLayout의 :

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/background_light"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" 
     android:adjustViewBounds="true" 
     android:src="@drawable/phone_orange" 
     android:contentDescription="@null" /> 

</RelativeLayout> 

ImageView in Relative Layout

있는 LinearLayout :

<?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:background="@android:color/background_light" 
    android:orientation="vertical" > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" 
     android:adjustViewBounds="true" 
     android:contentDescription="@null" 
     android:src="@drawable/phone_orange" /> 

</LinearLayout> 

ImageView in LinearLayout

왜 다르게 이러한 두 시나리오에서 ImageView 조치를합니까? ImageView이 항상 LinearLayout 케이스에있는 것처럼 동작하도록 만드는 방법이 있습니까 (LinearLayout으로 배치하는 것이 효과적이지만 깨끗한 솔루션은 아닙니다).

답변

12
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:maxHeight="100dp" 
android:adjustViewBounds="true" 

I got it there

+0

match_parent한다. – Petrakeas

+0

@Petrakeas 내가 대답을 업데이트, 내 애플 리케이션에서 그것을 시도, 잘 작동 –

+0

'android : maxHeight' 트릭을 할 것 같다! 그러나 왜 그것이 필요합니까? LinearLayout 내부는 모두 예상대로 작동합니다. RelativeLayout의 동작이 정확합니까? – Petrakeas

0

사용 이미지 뷰의 폭과 높이가 화상이 완전히 사라와

private void addView(Drawable d) { 
    RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.MATCH_PARENT,  RelativeLayout.LayoutParams.MATCH_PARENT); 
    ImageView iv = new ImageView(this); 
    iv.setImageDrawable(d); 
    iv.setAdjustViewBounds(true); 
    iv.setScaleType(ImageView.ScaleType.MATRIX); 
    iv.setLayoutParams(lparams); 
    rl.addView(iv); 
    iv.setOnTouchListener(this); 
}