안드로이드 버튼 drawable 크기 - andeuloideu beoteun drawable keugi

개요

이번에는 제가 xml파일을 편집할 때 이용하는 방식 중 하나인 layout_weight 방식을 알아보겠습니다.

설명

안드로이드로 어느 폰에서든 같은 비율을 사용하고자 할 때 사용할 수 있으며, 그냥 크기를 지정하는 것보단 효율적이고 안정적이라고 생각합니다.

사용방법은 아래와 같습니다.

horizontal으로 되어있으면 width를 match_parent로 설정한후 weight에 1을 설정해주고 같은 레이아웃에 있는 파일도 같게 해 주게 되면 어느 화면 에서는 비율이 같게 1:1로 나오게 됩니다.

layout(horizontal)예)

제가 간단한 점수판모양을 만든 코드도 다 weight을 사용하여 비율을 조정하였는데 코드를 보시고 이해하시면 됩니다.

점수판 코드

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="//schemas.android.com/apk/res/android" xmlns:app="//schemas.android.com/apk/res-auto" xmlns:tools="//schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_start_timer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="경기 시작!" /> <TextView android:id="@+id/txt_timer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="300" android:textSize="24sp" /> <Button android:id="@+id/btn_stop_timer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="종료!" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/p1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder" android:gravity="center" android:text="Player1" /> <TextView android:id="@+id/p2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder" android:gravity="center" android:text="Player2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/p1_point" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder" android:gravity="center" android:text="0" android:textSize="140sp" android:textStyle="bold" /> <TextView android:id="@+id/p2_point" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder" android:gravity="center" android:text="0" android:textSize="140sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/btn_p1_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="A+" android:textSize="12sp" /> <TextView android:id="@+id/p1_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="0" android:textSize="18sp" /> <Button android:id="@+id/btn_p1_add_cancel" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="A -" android:textSize="12sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/btn_p1_pe" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="P+" android:textSize="12sp" /> <TextView android:id="@+id/p1_pe" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="0" android:textSize="18sp" /> <Button android:id="@+id/btn_p1_pe_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="P-" android:textSize="12sp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/btn_p2_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="A+" android:textSize="12sp" /> <TextView android:id="@+id/p2_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="0" android:textSize="18sp" /> <Button android:id="@+id/btn_p2_add_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="A-" android:textSize="12sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/btn_p2_pe" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="P+" android:textSize="12sp" /> <TextView android:id="@+id/p2_pe" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="0" android:textSize="18sp" /> <Button android:id="@+id/btn_p2_pe_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="P-" android:textSize="12sp" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/p1_2point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="+2" android:textSize="14sp" /> <Button android:id="@+id/p1_2point_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="-2" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/p1_3point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="+3" android:textSize="14sp" /> <Button android:id="@+id/p1_3point_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="-3" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/p1_4point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="+4" android:textSize="14sp" /> <Button android:id="@+id/p1_4point_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="-4" android:textSize="14sp" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_weight="1" android:background="@drawable/boder2" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/p2_2point" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="+2" android:textSize="14sp" /> <Button android:id="@+id/p2_2point_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="-2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/p2_3point" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="+3" /> <Button android:id="@+id/p2_3point_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="-3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/boder" android:orientation="horizontal"> <Button android:id="@+id/p2_4point" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="+4" /> <Button android:id="@+id/p2_4point_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="-4" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>

Toplist

최신 우편물

태그