2014-05-13 3 views
0

나는 안드로이드를 처음 접했고 각 행에 3 개의 버튼이있는 2 개의 행이있는 레이아웃을 만들었습니다. 내 응용 프로그램의 UI가 모든 화면 크기와 호환되기를 바랍니다. 그래서 화면 폭과 높이를 px로 얻었습니다. 이전에 안드로이드에서 작업하지 않았기 때문에 px 또는 dp로 버튼을 조정해야 모든 장치에서 작동 할 수 있습니다. 감사합니다. .조정 방법 화면 크기에 따라 버튼 크기

답변

3

픽셀을 사용해서는 안됩니다. LinearLayoutlayout_weight을 사용해야합니다.

다음은 2 개의 동일한 크기의 버튼이 6 개있는 예입니다. 여기서 중요한 점은 android:layout_width="0dp"하고 여기에

<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" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button 1" /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button 2" /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button 3" /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button 4" /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button 5" /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Button 6" /> 
    </LinearLayout> 
</LinearLayout> 
+0

예 내가 사용 않았다있는 LinearLayout하지만 이것에 문제가 아무튼이었다, 각 행에 포함 된 3 개 버튼을 2 행과 레이아웃입니다 것입니다 버튼의 크기를 조정할 수 없습니다. (더 작은 레이아웃의 큰 버튼과 큰 버튼은 다른 버튼과 겹치거나 버튼을 완전히 표시하지 않습니다.) – Abbas

+0

6 개의 버튼이있는 레이아웃을 3 행으로 제공하는 답변을 업데이트했습니다. – tasomaniac

0

android:layout_weight="1"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="3" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="test" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="test" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="test" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="3" > 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="test" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="test" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="test" /> 
    </LinearLayout> 

</LinearLayout>