IT story

ImageView 주위의 원하지 않는 패딩

hot-time 2020. 6. 21. 19:23
반응형

ImageView 주위의 원하지 않는 패딩


모든 활동 / 조회에 헤더 그래픽을 포함시켜야합니다. 헤더가있는 파일을 header.xml이라고합니다.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  android:background="#0000FF" 
  android:padding="0dip">

  <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dip"
    android:layout_marginTop="0dip"
    android:layout_marginBottom="0dip"
    android:padding="0dip"
    android:paddingTop="0dip"
    android:paddingBottom="0dip"
    android:layout_gravity="fill"
    android:background="#00FF00"
    />
</FrameLayout>

메모 android:background="#00FF00"(녹색), 그냥 시각화 목적입니다.

다음과 같이 내 견해에 포함시킵니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  style="@style/white_background">

  <include layout="@layout/header" />
  (...)

따라서 실제로 시도하면 결과가 (오른쪽) 대신 왼쪽 이미지처럼 보입니다.

녹색 경계에 주목

(1)이-주황색 부분은 문제의 이미지 / 이미지보기입니다.
(2) 사랑하지 않는 녹색 테두리. 참고 : 일반적으로 녹색 영역은 투명합니다-설정했기 때문에 녹색 background입니다.

상단의 이미지 주위에 녹색 테두리가 있습니다. 그것은 ImageView의 일부이며 왜 그것이 있는지 또는 어떻게 제거 할 수 있는지 알 수 없습니다. 모든 패딩과 여백을 0으로 설정했습니다 (그러나 생략하면 결과는 동일합니다). 이미지는 480x64px jpeg *이며 res / drawable에 넣습니다 drawable-Xdpi.

(* jpeg, 그것은 오래된 png 감마 문제에 걸려 넘어 졌기 때문에-처음에는 녹색 테두리를 그림과 같은 오렌지색으로 만들어 문제를 해결했으며 색상이 일치하지 않았습니다.)

내 htc desire / 2.2 / Build 2.33.163.1과 에뮬레이터에서 시도했습니다. 또한 # android-dev의 누군가에게 문제를 설명했습니다. 그녀는 문제를 재현 할 수 있었지만 설명이 없었습니다. 빌드 대상은 1.6입니다.

@tehgoose 업데이트 :이 코드는 정확히 동일한 상단 + 하단 패딩 결과를 생성합니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  style="@style/white_background">

  <!-- <include layout="@layout/header" />  -->

  <ImageView
    android:src="@drawable/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#00FF00"
    android:layout_weight="0"
    />

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dip"
    android:layout_weight="1">

    (... rest of the elements)

  </LinearLayout>
</LinearLayout>

드디어!

<ImageView
  (...)
  android:adjustViewBounds="true" />

the adjustViewbounds attribute did the trick:

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

i stumbled upon it here. thanks for your help!


android:scaleType="fitXY"

It works for me.


It has to do with the image aspect ratio. By default, the system keeps the original aspect ratio of the image source. Which in most cases does not exactly match the layout or dimensions specified in the layout. Therefore,

  1. Either change the size of the image to fit the specified layout, or
  2. Use android:scaleType to fit the image. For example, you can specify android:scaleType="fitXY"

May be you can give specific height to imageView say 50dp or 40dp and adjust image to make green border dissappear.

Eg: android:layout_height="40dp"


android:layout_height="wrap_content"

You need to change it to "fill_parent"

프레임 레이아웃을 채우는 데 적합한 비율이되도록 이미지의 크기를 조정해야 할 수도 있습니다.

왜 거기에 프레임 레이아웃이 필요한지 이해하지 못합니다. 그것이 없으면 이미지는 어쨌든 화면을 채울 것입니다.

편집 : 예, 죄송합니다 .xml은 이미지 뷰의 높이입니다.


imageview xml 에서이 android : scaleType = "fitXY"사용


안드로이드가 원래 종횡비를 얻으려고 시도하기 때문에 추가 패딩이 자동 생성됩니다. 아래에서 시도하십시오

scaletype = "fitCenter"
android:adjustViewBounds="true"
android:layout_height="wrap_content"

더 잘 보이게하려면 imageview 주위에 모양을 제공해야합니다.

여기 내가 사용한 것 :

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--<gradient android:startColor="#FFFFFF" android:endColor="#969696"

    android:angle="270">
-->
<gradient android:startColor="#FFFFFF" android:endColor="#FFFFFF"

    android:angle="270">
</gradient>
    <stroke android:width="2dp" android:color="@color/graycolor" />
<corners android:radius="2dp" />
<padding android:left="5dp" android:top="5dp" android:right="5dp"
    android:bottom="5dp" />

참고 URL : https://stackoverflow.com/questions/5830503/unwanted-padding-around-an-imageview

반응형