2011-07-27 4 views
0

내 애플 리케이션 디자인에서 테이블을 사용하고 싶지만 안드로이드의 XML 레이아웃에서 HTML과 달리 퍼센트를 사용하여 무언가의 너비를 설정할 수는 없다. Google에서 보면 내게 가치가 100까지 추가되는 한 동일한 기능에 대해 'layout_weight'를 사용할 수있는 것처럼 보입니다.이 질문의 정보 (http://stackoverflow.com/questions/3629331/android-trying-to -understand-androidlayout-weight) 내 레이아웃을 썼는데 제대로 작동하는 것 같았습니다. 나는이 레이아웃에 버튼을 추가 할 때 그러나이 같은 여기에 본 나누기 http://i.imgur.com/rCjKk.png안드로이드에서 "layout_weight"이해하기

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

    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

      <TableRow 
       android:id="@+id/tablerow1" 
       android:layout_width="fill_parent" 
       android:layout_height="0px" 
       android:layout_weight="10">  
      </TableRow> 

      <TableRow 
       android:id="@+id/tablerow2" 
       android:layout_width="fill_parent" 
       android:layout_height="0px" 
       android:layout_weight="80"> 
      </TableRow> 

      <TableRow 
       android:id="@+id/tablerow3" 
       android:layout_width="fill_parent" 
       android:layout_height="0px" 
       android:layout_weight="10"> 

        <TableRow 
         android:id="@+id/tablerow4" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" 
         android:layout_weight="50"> 
        </TableRow> 

        <TableRow 
         android:id="@+id/tablerow5" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" 
         android:layout_weight="50">  
        </TableRow>       

      </TableRow> 

</TableLayout> 

</LinearLayout> 

:

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

    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

      <TableRow 
       android:id="@+id/tablerow1" 
       android:layout_width="fill_parent" 
       android:layout_height="0px" 
       android:layout_weight="10">  
      </TableRow> 

      <TableRow 
       android:id="@+id/tablerow2" 
       android:layout_width="fill_parent" 
       android:layout_height="0px" 
       android:layout_weight="80"> 
      </TableRow> 

      <TableRow 
       android:id="@+id/tablerow3" 
       android:layout_width="fill_parent" 
       android:layout_height="0px" 
       android:layout_weight="10"> 

        <TableRow 
         android:id="@+id/tablerow4" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" 
         android:layout_weight="50"> 

          <Button 
           android:id="@+id/button1" 
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent"                  
           android:text="Speak"> 
          </Button> 

        </TableRow> 

        <TableRow 
         android:id="@+id/tablerow5" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" 
         android:layout_weight="50"> 

          <Button 
           android:id="@+id/button2" 
           android:layout_width="fill_parent" 
           android:layout_height="fill_parent"         
           android:text="Listen"> 
          </Button> 

        </TableRow>       

      </TableRow> 

</TableLayout> 

</LinearLayout> 

이 사람이 나에게 http://i.imgur.com/sKxbv.png 설명 할 수 여기에

는 & 같은 XML 파일을 모습입니다 왜 이래? 나는 단순히 10 %, 80 %, 10 %에서 3 개의 행을, 그리고 나서 마지막 행의 너비의 50 %를 차지하기를 원합니다.

+0

하단의 버튼 만 원하십니까? 아니면 특별히 전체 화면의 10pcs가되어야합니까? –

답변

2

나는 문자 그대로 TableLayout에 대해 작동하지 않는 이유가 전혀 없으므로 많은 노력을 기울여 작동하지 못했습니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:orientation="vertical"> 
<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp"></LinearLayout> 
<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8"></LinearLayout> 
<LinearLayout 
    android:id="@+id/linearLayout3" 
    android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp"> 
    <Button 
     android:text="Button" 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_weight="1"></Button> 
    <Button 
     android:text="Button" 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:layout_weight="1"></Button> 
</LinearLayout> 

+0

고마워요. 완벽하게 작동합니다. 정말 이상한 그것은 비록 테이블과 함께하지 않았다 .. –

0

내가 올바르게 이해한다면, 버튼이 부모를 밀어 붙이는 것이 문제일까요?

그렇다면 버튼 대신 android:layout_height="wrap_content"을 사용해보세요.

+0

그래, 그게 문제 야. android : layout_height = "wrap_content"할 때 버튼이 올바른 크기로 만들어 지지만 하단 행은 여전히 ​​두 번째 그림과 같은 크기로 맨 위에 매달려 있습니다. –

0

이 작동하지 않는 이유는 당신이 그 다음이다 TableRow 크기를 제공하기 위해 노력하고 있다는 것입니다 : 당신이 그들과 함께 살 수있는, 그래서 만약 그러나,은 LinearLayouts`괜찮 작동 내부 콘텐츠의 크기에 추가됩니다. TableLayout을 사용하여 수행해야하는 작업은 컨텐츠 뷰가 크기를 제어하게합니다. 문제는 높이에 대한 가중치와 상관없이 버튼 공간을 가로로 공유 할 수있는 무언가가 필요하다는 것입니다.

실제로 TableRow는 단순히 수평 LinearLayout이므로 하단 TableRow 내부에 두 개의 TableRow가있는 것은 불필요하므로 단일 LinearLayout으로 바꾸면됩니다.

최상위 2 행에 어떤 콘텐츠가 있는지 알지 못해서 가장 좋은 것을 말하기는 어렵지만 TableLayout은 아마도 당신이 원하는 것에 가장 적합하지 않을 수 있습니다.

관련 문제