마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

레이아웃 질문드립니다. 중간에다가 적당한 크기로버튼을 만들고싶어요!

0 추천

안녕하세요. 구현하려고 하는 레이아웃은 이렇게 생겼습니다.

 

 

원하는 레이아웃은 딱 화면의 중간에다가 4개의 이미지버튼을 표시하는거거든요.

버튼의 크기같은 경우는 화면의 비율에 맞춰서 할거구요.(가령 가로20% 세로15%)

 

궁금한게 정확히 가운데다가 4개의 버튼을 배치하려면 어떻게 해야하나요?

 

제가 찾아본소스로는..

http://stackoverflow.com/questions/16884022/layout-4-buttons-in-android

http://stackoverflow.com/questions/12555106/android-layout-with-4-squares-and-a-button-inside-each-square

가 있는데 두페이지모두 완전 중간에다가 배치시킬수있는 방법은 아닌것같습니다..

 

이렇게 하려는이유가 기기의 화면크기가 다르더라도 적당한 비율과 적당한 위치를가진 버튼을 보고싶기때문인데요..

아직 입문단계라서.. 조언좀 해주시면 쿨럭.. 좋겠습니다.

익명사용자 님이 2014년 3월 23일 질문

1개의 답변

+1 추천

가중치 작업을 하신다면 보통은 LinearLayout로 작업을 하실텐데요.

저는 몇가지 방법을 사용하는데 마진 자체도 LinearLayout으로 잡는 경우가 있지만 가중치 작업이 많아지면

해상도에 따라 Activity 로딩속도가 현저히 줄어들죠.

 

부모 Layout를 아래와 같이 한번 해보시겠어요?

 

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >
 
<!-- 버튼 작업 -->
</LinearLayout>
 
layout_gravity : 뷰 자체의 정렬 상태를 지정, 부모뷰에서 현재의 뷰를 어디에 놓을 것인지를 지정
 
버튼의 부모 레이아웃의 부모의 기준으로 중앙 배치 후 버튼을 정렬하시면 될것 같습니다.
익명사용자 님이 2014년 3월 23일 답변
아. 레이아웃 그레비티를 사용해보았지만 어떤식으로해야 버튼을 4개를 양옆그리고 아래로 나뉠수있는거죠? linear로 할경우 수평정렬밖에 되지가 않더라구요..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center" >
 
<!-- 버튼 작업 -->

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

</LinearLayout>

어라.. 이렇게하니까 된것같습니다.
혹시 다른방법도있다면 알려주시면감사하겠습니다.
레이아웃 그레비티는 버튼의 부모의 위치를  잡는거구요
버튼을 추가로 정렬할려면 각각의 버튼의 그레비티를 추가하시면  되요.
-위의 익명글의 주인공입니다.
...