2011-07-27 15 views
0

私のアプリケーション設計ではテーブルを使用したいと思いますが、AndroidのXMLレイアウトのHTMLとは異なり、パーセントを使って何かの幅を設定することはできません。 Googleで見ると、値が100になる限り同じ機能にlayout_weightを使用できるようです。この質問の情報(http://stackoverflow.com/questions/3629331/android-trying-to -understand-androidlayout-weight)私は自分のレイアウトを書いて、うまくいくように見えました。誰も私に説明できhttp://i.imgur.com/sKxbv.pngAndroidの「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"> 

          <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/rCjKk.png

<?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ファイルは&のようにそれが見えるものですなぜこれは?私は単純に10%、80%、10%で3行、そして最後の行の幅の50%を2つ使います。

+0

ボタンを底にしたいのですか、それとも画面全体の10%にする必要がありますか? –

答えて

2

なぜそれがTableLayoutで動作しないのか、私は文字通り考えがありませんでした。私は多くを試しましたが、うまく動作しませんでした。あなたも一緒に暮らすことができるので、もししかし、それは、 `LinearLayouts大丈夫作品:

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

それは問題です。私がアンドロイドをするとき:layout_height = "wrap_content"それはボタンを正しいサイズにしますが、一番下の行はボタンが上からぶら下がっているのと同じサイズです。 –

0

これが動作しない理由は、あなたがしているのTableRowサイズを与えるためにしようとしているということです内部のコンテンツのサイズに追加されます。 TableLayoutで行うべきことは、コンテンツビューでサイズをコントロールできるようにすることです。あなたの問題は、高さの重み付けとは関係なく、ボタンのスペースを水平に共有できるものが必要なことです。

実際には、TableRowは単純に水平のLinearLayoutなので、下部のTableRow内に2つのTableRowを配置することは余計です。ただ1つのLinearLayoutで置き換えるだけです。

トップ2列にどのようなコンテンツがあるのか​​わからなくても、何が最善のものかはっきり言えませんが、TableLayoutはおそらくあなたが望むものには適していません。

関連する問題