안드로이드 레이아웃 질문입니다

조회수 475회

왼쪽 이미지 처럼 배치하려고 android:layout_weight를 1씩 주면, 오른쪽 이미지 처럼 ImageView 비율이 안맞게 나옵니다. TextView도 텍스트 길이가 일정 이상을 넘어가면 비율이 안맞게 됩니다.

android:layout_width와 android:layout_height 크기를 고정하면 되기는 한데, 그렇게하면 디스플레이 크기가 달라지면 호환이 안될 것 같아서요...

ImageView 나 TextView가 고정된 비율 안에서 표시되도록 하고싶은데 방법이 있을까요?

아니면, 다양한 사이즈의 디스플레이에 맞춰 ui를 개발할 때 보통 어떤 방식을 쓰나요?

이미지 이미지


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:src="@drawable/emergency_64"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:text="text1"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="40dp"
            android:textColor="@color/colorText"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textview1"
            android:text="text2"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="40dp"
            android:textColor="@color/colorText"/>
    </LinearLayout>

2 답변

  • LinearLayout에서 layout_weight 값을 줄 때 layout_width 값이 wrap_content로 설정되어있으면, 뷰의 너비가 커질 경우 고정된 크기 이상을 넘어가게 됩니다. 이를 해결하기 위해서 layout_width 값을 0dp로 설정하시면 깔끔히 해결됩니다.

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/textview1"
        android:text="text2"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="40dp"
        android:textColor="@color/colorText"/>
    

    Orientation이 Vertical일 때도 마찬가지로 layout_height를 0dp로 설정하시면 됩니다.

    wrap_content일 때: 사진

    0dp일 때: 사진

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)