2014-11-06 2 views
0

동일한 너비와 높이로 4 개의 단추를 수평으로 만들려고합니다. 이제 너비를 0dp로 설정하고 가중치 = "1"로 설정하면 모든 버튼이 수평으로 동일한 폭으로 흐려 지지만이 요소의 높이를 같게 설정하고이를 수행하는 방법을 알 필요가 없습니다.사각형 단추를 만들고 horizintaly 방해하는 방법

제발,이 문제를 해결하도록 도와주세요. 내 XML 레이아웃 아래

는 :

<LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" android:id="@+id/lin_layout"> 
     <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="VK" 
       android:id="@+id/btn1" 
       android:layout_weight="1" 
       android:layout_marginRight="5dp"/> 
     <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="ЮД" 
       android:id="@+id/btn2" 
       android:layout_weight="1" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp"/> 
     <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="KE" 
       android:id="@+id/btn3" 
       android:layout_weight="1" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp"/> 
     <Button 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:background="@drawable/contact_bg" 
       android:id="@+id/btn4" 
       android:text="MI" 
       android:layout_weight="1" android:layout_marginLeft="5dp"/> 
    </LinearLayout> 
+1

그냥 부모의 LinearLayout의 높이로 DP 치수 값을 제공하고 다른 장치에 폭 알고하지 않기 때문에, 나는이 작업을 수행 할 수 – Endor

+0

를 match_parent 각 버튼의 높이를 설정합니다. – berliozz

답변

0

하지만 버튼이 선형 레이아웃에 대해 모두 같은 높이가되고 싶은 경우는, 같은 일의 높이에 따라 달라은 android: layout_height = "match_parent"과 버튼을 설정 선형 레이아웃 인 만큼 높을 것입니다.

예하면

<LinearLayout 
    android:id="@+id/lin_layout" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/btn1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="VK" /> 

    <Button 
     android:id="@+id/btn2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="ЮД" /> 

    <Button 
     android:id="@+id/btn3" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="KE" /> 

    <Button 
     android:id="@+id/btn4" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="MI" /> 
</LinearLayout> 
0

그러니까 버튼의 높이는 그 폭이 동일해야한다. 이미지보기 : 내 경험에

enter image description here

+0

올바른 해결 방법은 모두 코드 스타일 출력을 사용하는 것입니다. – berliozz

0

을 당신이 XML에 전적으로 요구하고 일을하는 것은 불가능합니다, 나는 당신이 동적으로해야 할 것 같군요. 모든

먼저 android:layout_height="match_parent"

<LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" android:id="@+id/lin_layout"> 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="VK" 
      android:id="@+id/btn1" 
      android:layout_weight="1" 
      android:layout_marginRight="5dp"/> 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="ЮД" 
      android:id="@+id/btn2" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp"/> 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:text="KE" 
      android:id="@+id/btn3" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp"/> 
    <Button 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:background="@drawable/contact_bg" 
      android:id="@+id/btn4" 
      android:text="MI" 
      android:layout_weight="1" android:layout_marginLeft="5dp"/> 
</LinearLayout> 

모든 버튼의 높이를 변경하고 나중에 LinearLayout의 폭을 얻고 그것 폭의 1/4에 높이의 설정.

lin_layout.getLayoutParams().height = lin_layout.getLayoutParams().width/4; 
+1

op가 버튼 주위에 여백을 설정 했으므로 화면 너비의 1/4을 사용하는 대신 버튼의 너비를 높이로 사용해야합니다. – Endor

관련 문제