«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

[위미르 개발팀] Android, iOS , Web 제작

[안드로이드 레이아웃]background와 src의 차이 본문

개발자료/Android

[안드로이드 레이아웃]background와 src의 차이

위미르개발팀 2017. 9. 14. 10:00

레이아웃을 짜면서 뷰에 이미지를 넣을때가 있는데, 많은분들이 background와 src의 차이에 대해서 제대로 이해하지 않고

사용하고있습니다.


보통의 경우에는 문제가 되지 않지만, 뷰에 padding이 먹혀있는경우, 그리고 이미지의 비율과 이미지를 담을 뷰의 비율이 맞지않을때 문제가 생깁니다.


background값은 뷰 전체의 배경을 설정하는것으로 무조건 뷰를 꽉 채우게 되고 이때 뷰에 padding이 걸려있어도 이를 무시하고 뷰 전체의 배경을 깔게 됩니다.


반면 src의 경우 background와 다르게 뷰에 컨텐츠를 넣어주는 개념이라서 padding이 걸려있다면 패딩만큼 안으로 밀려서 이미지가 설정되게 됩니다.




background를 사용하여 이미지를 넣을경우 원하는대로 되지않을수도 있고, 전체를 덮는 이미지를 배경으로 글자를 넣는다던지 하는일은 거의 없기때문에 뷰에 이미지를 넣을때는 왠만하면 backgroud는 사용하지않는것이 좋습니다. 


실험에 사용할 넝쿨 아이콘 이미지 입니다


넝쿨




실제 적용 화면입니다



background로 적용 


background

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#AEF">
 
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:padding="10dp"
        android:background="@drawable/ic_launcher" />
</LinearLayout>
cs



패딩을 설정하였음에도 뷰를 꽉 채우고 있는 모습입니다.

그리고 adjustViewBounds또한 먹히지 않아 이미지의 비율이 맞지 않습니다.


>



src로 적용

src

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#AEF">
 
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:padding="10dp"
        android:src="@drawable/ic_launcher" />
</LinearLayout>
cs


패딩이 적용 되었고, adjustViewBounds가 적용되어 이미지의 비율이 맞게 들어갔습니다.


둘의 차이를 알고 사용하면 더욱 내가 원하는 레이아웃을 만들기 쉽겠죠?







저희 위미르에서는 모바일 어플리케이션(Android/iOS), Web 개발을 해드리고 있습니다.


문의 전화 : 070-4177-3962






Comments