2011-03-28 2 views
26

화면 중간에 3 개의 버튼을 표시하고 길이가 다른 텍스트 라벨을 갖지만 3 개의 버튼이 모두 같은 너비가되도록하고 싶습니다.동일한 너비의 단추를 만드는 방법은 무엇입니까?

다른 길이의 텍스트 레이블이있는 3 개의 버튼을 추가하면 다양한 너비의 버튼이 생성됩니다. -

1 layout_weight를하고 모든 버튼에 0dip하는 layout_width 그들이 전체 화면을 채우기 위해 동등하게 스트레칭 원인을 설정
default button width wraps contents

:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center"> 
    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="JKLM" /> 
</LinearLayout>

기본 버튼의 폭은 내용을 랩 폭. 내가 원하는 것을 위해, 그러한 버튼은 특히 커다란 스크린에서, 너무 크다. 상위의 LinearLayout에 weightSum 대한 다른 값을 설정

화면 전체를 채우는에서 버튼을 중지하는데 사용될 수있다 -
layout weight 1 buttons fill screen width

는 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center"> 
    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="JKLM" /> 
</LinearLayout>

레이아웃 중량 1 버튼을 화면 폭을 채우 그러나 버튼이 대형 화면 장치에서 화면의 많은 부분을 차지하지 않기를 바랄 때 나는 이것이 내가 원하는 길이라고 생각하지 않습니다. 예를 들어, weightSum을 사용하여 세 개의 버튼을 화면 너비의 절반을 집합 적으로 차지하도록 설정할 수 있습니다. 작은 화면에서는 괜찮아 보이지만, 큰 화면에서는 버튼이 화면 너비의 절반을 차지하게됩니다. 버튼은 단순히 내가 원하는 것보다 훨씬 커야합니다. 아마도 최종 해결책은 여러 화면에 서로 다른 레이아웃 파일을 사용하는 것이지만이 방법을 사용하지 않을 것입니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center" 
    android:weightSum="5"> 
    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="JKLM" /> 
</LinearLayout>

체중 합계 5 작은 화면 :
weight sum 5 small screen

체중 합계 5 대형 스크린 :
weight sum 5 large screen

- 나는 또한 TableLayout을 많은 일을했지만, 'didn를

LinearLayout을 사용하는 것보다 나은 것을 얻으십시오.

GridView는 사용하기에 번거롭기 때문에 아직 사용해 보지 않았습니다.

그렇다면 가장 긴 레이블이있는 단추의 내용을 맞추기 위해 필요한 너비가 동일한 위치에서 동일한 너비의 단추를 만드는 방법은 무엇입니까?

모든 조언을 주시면 감사하겠습니다.

+1

방법 사용자 정의 레이아웃을 작성하는 방법에 대한? – bigstones

답변

5

아마도 최종 솔루션은 단순히 가질 수 없습니다 (I 검색하고이 질문에 여러 번 물어 대답 발견,하지만 난 달성하기 위해 노력하고있어 해결 발견 답변의 어느 것도. 한) 다른 화면의 레이아웃 파일은 다르지만 차라리이 경로를 사용하지 마십시오.

많은 프로그래머는 이와 같은 상황을 처리하기 위해 res/layout/res/layout-large/을 사용합니다. 3 개의 단추 중 제한된 경우 대안이있을 수 있지만 일반적으로 사용자 인터페이스는 그렇게 단순하지 않습니다.

그렇다면 가장 긴 레이블이있는 단추의 내용을 맞추기 위해 필요한 너비가 넓은 것과 같은 너비의 단추를 만드는 방법은 무엇입니까?

"우선적으로"[요구 사항] 요구 사항을 충족 시키려면 사용자 정의 레이아웃 클래스를 만들어야합니다. 대시 보드 패턴의 경우 Here is one related to it을 시작점으로 사용할 수 있습니다.

+0

Arg. [안드로이드는 내가 원하는 것을 할 수 없다는 것을 마지 못해 받아 들인다. 크기가 같고 높이가 같고 가중치가없는 버튼을 설정하는 기능이 불충분합니다.] –

-1

이 스레드를 읽고, 다음 몇 가지 시행 착오를하고 android:layout_width="180px"가 가능 매개 변수입니다 것으로 나타났습니다. 지금 말했듯이, 나는 이것에 비틀 거리고, 당신의 3 개의 단추 대본을 해결하기 위하여 이것을 이용하지 않았다.

처음 게시 한 이후로 변경된 사항 일 수 있습니다. 비록 1.5 버전을 만들기 위해 이것을 시도했지만. 당신이 알고있는 경우 넓은 단추 텍스트는 당신이 당신의 버튼을 설정 android:ems 속성을 사용할 수있을 것입니다 무엇을 사전에

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:id="@+id/dateText" 
     android:text="\n\nClick for Date\n\n" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="180px" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Get the Time" 
     android:onClick="showNewDate" /> 

</LinearLayout> 
+0

1.'px'가 아니라'dp'를 사용해야합니다. 2. 너비를 특정 dp 값으로 설정해도 버튼 내부의 텍스트가 적합하지 않을 수 있습니다 (텍스트에 따라 다름). –

3

: 그 다음은

이 전체 main.xml입니다 :-) ... 충분히 오래 폭.

<Button 
    android:id="@+id/my_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minEms="5" 
    android:text="@string/my_button" /> 

그것은 완벽 하진하지만 내가 끝없이 레이아웃으로 주위 하구없이 본 모습을 달성하기 위해 찾은 가장 쉬운 방법입니다.

0

내 옆에있는 메모가 하나뿐입니다.

런타임시 추가되는 버튼 (또는 간단한 코드 변경이있는 다른보기)의 너비 (또는 높이)를 설정해야하는 경우 아래 코드를 사용할 수 있습니다 (방향에 의존하지 않음).) :

여기
public void AlignButtons(){ 
    llModules = (LinearLayout) findViewById(R.id.llModules); 

    ViewTreeObserver viewTree = llModules.getViewTreeObserver(); 
    viewTree.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
     public boolean onPreDraw() { 

      int childcount = llModules.getChildCount(); 
      int maxWidth = 0; //int maxHeight = 0; 

      String fontPath = "fonts/Isabella-Decor.ttf"; 
      Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); 

      for (int i = 0; i < childcount; i++) { 
       LinearLayout v = (LinearLayout)llModules.getChildAt(i); 
       View vv = v.getChildAt(0); 
       if (vv instanceof Button) { 
        int width = vv.getMeasuredWidth(); 
        maxWidth = (maxWidth > width) ? maxWidth : width; 
        //int height = vv.getMeasuredHeight(); 
        //maxHeight = (maxHeight > height) ? maxHeight : height; 
       } 
      } 
      for (int i = 0; i < childcount; i++) { 
       LinearLayout v = (LinearLayout)llModules.getChildAt(i); 
       View vv = v.getChildAt(0); 
       if (vv instanceof Button) { 
        LayoutParams params = ((Button) vv).getLayoutParams(); 
        params.width = maxWidth; 
        //params.height = maxHeight; 
        ((Button) vv).setLayoutParams(params); 

        // Applying font 
        ((Button) vv).setTypeface(tf); 
       } 
       vv = v.getChildAt(1); 
       if (vv instanceof TextView) { 
        // Applying font 
        ((TextView) vv).setTypeface(tf); 
       } 
      } 

      return true; 
     } 
    }); 
} 

, 내 경우 :

llModule - 우리는 (v.getChildAt(0);를 정렬해야하는 것과 다른 레이아웃을 포함하는 몇 가지 레이아웃) 및 기타 (v.getChildAt(1);).

LinearLayout v = (LinearLayout) llModules.getChildAt (i); - 정렬 할 버튼의 레이아웃을 얻습니다.

다른 부분은 분명히 이해할 수 있습니다.

이 코드의 두주기는 완전히 동일하지만, 실행 시간을 단축하기 위해 이들을 결합하는 방법을 여전히 상상할 수 없습니다.

0

이 코드를 사용하면 도움이 될 것입니다.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:weightSum="3"> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Get the Time" 
    android:onClick="showNewDate" /> 
<Button 
    android:id="@+id/button2" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Get the Time" 
    android:onClick="showNewDate" /> 
<Button 
    android:id="@+id/button3" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Get the Time" 
    android:onClick="showNewDate" /> 

관련 문제