2011-08-08 4 views
36

좋아, 내가 선형 레이아웃에 두 개의 버튼이 있습니다두 개의 버튼을 화면 크기에 관계없이 같은 너비로 설정 하시겠습니까?

<LinearLayout android:id="@+id/linearLayout1" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent"> 
     <Button android:id="@+id/aktiviraj_paket" 
       android:text="Aktiviraj" 
       android:layout_height="40sp" 
       android:layout_width="160sp" 
       android:background="@drawable/my_border3" 
       android:onClick="myClickHandle"></Button> 
     <Button android:id="@+id/deaktiviraj_paket" 
       android:text="Deaktiviraj" 
       android:layout_height="40sp" 
       android:layout_width="fill_parent" 
       android:background="@drawable/my_border3" 
       android:onClick="myClickHandle"> 
     </Button> 
</LinearLayout> 

그래서 일이 내가 만든 내가 두 버튼에 부모를 채워 사용하는 경우, 그들은 서로 하나되어 있으므로되는 첫 번째 버튼의 160sp 폭을하고, 두 번째는 fill_parent입니다. 이것이 4 인치 화면 이하로 표시되면 버튼의 크기는 동일하지만, 태블릿 (10 인치)에서이 버튼을 시도하면 첫 번째 버튼이 160sp 너비로 유지되고 두 번째가 화면 끝까지 펼쳐집니다 (fill_parent로 인해). 이걸 만들 수있어, 화면의 크기에 상관없이 양쪽 버튼이 짝수 크기 일 수 있습니까? 각 버튼에

답변

92

사용 android:layout_weight="1". 양쪽 모두에 android:layout_width="0dp"을 설정하십시오. 이제 두 버튼의 가중치가 같으므로 각 버튼의 부모 너비가 절반이됩니다.

당신은 더 여기에서 찾을 수 있습니다 : http://developer.android.com/guide/topics/ui/layout/linear.html

+0

감사합니다. 이것은 나를 도왔습니다 :) – Goran

+0

고마워요 ... 내 1-2 시간을 절약했습니다 :) – parul

3

설정 : 모두 Button들에

android:layout_weight="0.5" 
android:layout_width="0dp" 
4
Display display=getWindowManager().getDefaultDisplay(); 
    int width=display.getWidth(); 
    btn1.setWidth(width/2); 
    btn2.seTwidth(width/2); 

XML 파일에 아무것도 먼저 그들은 정확히 동일한

+2

이 솔루션은 XML 리소스의 레이아웃을 정의하는 정신에 어긋납니다. Che Jami의 솔루션이 더 좋습니다. –

+0

이 솔루션은 xml보다 나을 때 가장 좋습니다. 고맙습니다. – Jack

14
을 보이는 것 모든 장치에 이제 모두 버튼 에 폭의 절반을 설정 장치의 폭을 찾을 수 설정

당신이하고자하는 일은 모든 버튼을 가장 넓은 버튼의 너비로 설정하고, 무게를 설정하는 것은 그렇게하지 않을 것입니다. 오히려, 당신은 TableLayout을 모든 버튼을 넣을 수 있습니다 :

<TableLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/short_text" /> 
     </TableRow> 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/enter_manually" /> 
     </TableRow> 
    </TableLayout> 

이 레이아웃을 2 개 버튼, 다른 상단에 하나, 동일한 폭을 표시합니다. 수평 방향으로 설정 한 다음 함유 선형 레이아웃 로이드 있어야 레이아웃에 layout_weight = "1"

+0

이 대답은 열심히 잤다. –

+0

나는 날씨에 그 감사함이나 풍자를 얻지 않았다! – Sameer

+0

나는 weight 속성을 사용하는 대신이 방법을 사용하는 것을 선호한다. 그것은 분명히 과소 평가되었습니다. – TheLearner

1
android:layout_weight="0.5" 
android:layout_width="0dp 

0

세트 로이드 작업있다.

로이드 : 그리고 내부 버튼은 다음 있어야 layout_width = "0dp"

로이드 :에 layout_weight = "0.5"

0
<LinearLayout 
    android:id="@+id/layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:layout_weight="1" 
     android:text="One" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:layout_weight="1" 
     android:text="Two" /> 
</LinearLayout> 

These is the example for equal size buttons for side by side can be done from above code

android:layout_weight 

가 사용되어 LinearLayout의 모든 자식에 대해 버튼이나 등량의 공간을 할당하십시오.

참고 : 선형 레이아웃에서만 작동합니다.

+0

답장에 더 많은 설명을 추가하십시오. 너 뭐야? 왜 당신의 대답은 옳은가요? 너는 무엇을 고쳐 주 었는가? 귀하의 답변을 살펴보고 더 자세한 내용을 추가하십시오.감사. – cramopy

+0

괜찮은 @cramopy는 자세한 설명이 필요한 경우 알려주세요. –

관련 문제