2013-05-09 4 views
10

私は6つのボタンでレイアウトを作成しようとしています。コードでは、6つのボタンを動的に作成しますが、2つはライン用ですが、ボタンは後者を満たす画面のサイズに合わせる必要があります。どうすればいいですか?ウィンドウタイルのような6つのボタンを使ってレイアウトを作成する方法

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

<LinearLayout 
    android:layout_width="match_parent" 
     android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" /> 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
     android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
     android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

</LinearLayout> 

+0

あなたが今見ているものと期待しているものを載せてください。それ以外の場合は答えが難しいでしょう。 – Siddharth

+0

このコードは私にこのエラーが表示されるので、何も表示されません。「疑わしいサイズ:これはビューを非表示にし、LinearLayoutのlayout_weightで使用する必要があります」android:layout_height = "0dip" – bisemanu

+0

「0dip」が問題です。 – Siddharth

答えて

16

を見てみるべきだと思い垂直LinearLayout子供と同じ体重の3行は、各行が水平LinearLayout持つ2人の子供であることと全部が満たされていることを確認する同じ重量。 6つのボタンでは、パフォーマンスは問題ではありません。

パフォーマンスが問題になる場合は、行をRelativeLayoutとし、支柱を使用して半分に分割し、それに基づいて2つの子を配置することができます。

私はストラット言うとき、私はこのことを意味:

<View android:id="@+id/strut" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_centerHorizontal="true"/> 

更新:あなたはLinearLayout Sをしようとしているので 、ここであなたは高さと幅を扱うことができます方法は次のとおりです。

LinearLayoutを持つことができます。

android:layout_width="match_parent" 
android:layout_height="match_parent" 
人の

3人のLinearLayout子どもたちが持っています:

android:layout_width="match_parent" 
android:layout_height="0dip" 

Button sがあります:

android:layout_width="0dip" 
android:layout_height="match_parent" 

あなたが気づくことができるように、我々は重量が(いずれかの高さの場合に適用されているプロパティの0dipを持っています親が垂直方向、または親が水平方向の場合は幅)、空間を埋めるために成長する必要があります。

ここで完全なXML(ボタンはドローアブルを含めるので、あなたを追加して自由に感じることはありません)です:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:orientation="horizontal" 
     android:layout_weight="1" > 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:orientation="horizontal" 
     android:layout_weight="1" > 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:orientation="horizontal" 
     android:layout_weight="1" > 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 

そして結果: enter image description here

+0

私はXMLコードで自分の質問を編集しました。しかしこのコードでは、画面に正しく表示されないボタンの表示に問題があります。 – bisemanu

+0

アンドロイド:layout_width = "0dip"は、それらの 'Button'のすべての幅に必要なものです。 – Voicu

+0

私の質問を編集しましたが、うまくいかない – bisemanu

3

は、私はあなたが私が使用したいGridView

+0

Windowsのタイルにもセルが分割されていますが、GridViewはどのように役立ちますか? – Siddharth

0

使用GridLayoutのは!これはこの状況に最適です

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="350dp" 
    android:layout_margin="0.5dp" 
    android:columnCount="2" 
    android:rowCount="3" > 

    <Button 
     android:id="@+id/b_1" 
     /> 

    <Button 
     android:id="@+id/b_2" 
     /> 

    <Button 
     android:id="@+id/b_3" 
     /> 

    <Button 
     android:id="@+id/b_4" 
     /> 

    <Button 
     android:id="@+id/b_5" 
     /> 

    <Button 
     android:id="@+id/b_6" 
     /> 
</GridLayout> 
0

私はAndroidの.v7ライブラリを使用しています。このxmlは2列、3行のレイアウトを作成して画面全体を塗りつぶしてくれました:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/lib/android.support.v7.widget.GridLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:grid="http://schemas.android.com/apk/res-auto" 
    android:layout_margin="0.5dp" 
    app:columnCount="2" 
    app:rowCount="3" 
    app:useDefaultMargins="true"> 

    <Button 
     android:id="@+id/b_1" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="0" 
     grid:layout_column="0" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_2" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="0" 
     grid:layout_column="1" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_3" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="1" 
     grid:layout_column="0" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_4" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="1" 
     grid:layout_column="1" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_5" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="2" 
     grid:layout_column="0" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_6" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="2" 
     grid:layout_column="1" 
     android:text="Hellooo"/> 

</android.support.v7.widget.GridLayout> 
関連する問題