2016-11-16 8 views
1

enter image description here 画像が128x128の解像度でテーブルビューに挿入されたとき(画面全体をカバーする)私はそれらを256x256にリサイズし、スクリーン全体をカバーします。 9枚のパッチ画像などを使って画面全体を覆い、必要に応じてどの画面解像度でも動作させることはできませんか?密度を使用する必要がありますか?テーブルビューに画像を挿入すると画面全体が覆われない

私はfitXYを私のビューに設定しようとしましたが、私の問題は解決しませんでした。必要に応じて

は私はわからないが、ここに私のコードです:

私は4つのGIF画像ボタンを作成し、GIFイメージボタンのリストにそれらを追加しました:

for (int i = 0; i < 4; ++i) { 
    GifImageButton myButton = new GifImageButton(); 
    myButton.setBackgroundResource(drawables[i]); // add some drawable to the button background 
    myButton.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1.0f)); 
    listButtons.add(myButton); 
} 

は、その後、私はtablelayoutを作成したが(プログラムで行= 2、列= 2)と私のレイアウトにこれを追加しました:

MyTableLayout tableLayout = new MyTableLayout(this); 
tableLayout.createTableLayoutOfButtons(tableRows /*=2*/, tableCols /*=2*/, listButtons); 
mylinearLayout.addView(tableLayout); 

私MyTableLayoutクラスは次のとおりです。

public class MyTableLayout extends TableLayout { 

    public MyTableLayout(Context context) { 
     super(context); 
    } 

    // indexListButtons is the current index of the listButtons elements. so as long as there are buttons, we will add them to the table rows 
    int indexListButtons = 0; 
    public void createTableLayoutOfButtons(int numRows, int numCols, List<GifImageButton> listButtons) { 
     setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT)); 

     for (int i = 0; i < numRows; ++i) { 
      TableRow tableRow = new TableRow(getContext()); 
      tableRow.setGravity(Gravity.CENTER); 

      tableRow.setLayoutParams(new TableLayout.LayoutParams(
        TableLayout.LayoutParams.MATCH_PARENT, 
        TableLayout.LayoutParams.MATCH_PARENT)); 

      for (int j = 0; j < numCols; ++j, ++indexListButtons) { 
       // indices 0, 1, 2, 3 
       if (indexListButtons < listButtons.size()) { 
        tableRow.addView(listButtons.get(indexListButtons)); 
       } 
       // indices bigger than 3 don't exist so insert empty views in order to make each of the views in the same size 
       else { 
        // not enough buttons 
        TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1.0f); 
       Button btn = new Button(getContext()); 
       btn.setLayoutParams(params); 
       btn.setVisibility(View.INVISIBLE); 
       tableRow.addView(btn); 
       } 
      } 
      addView(tableRow); 
     } 
    } 
} 

======

EDIT

======

のVeselinトドロフ、これはあなたの意味だったなら、私は理解してみましょう。おかげで..

public class MyTableLayout extends TableLayout { 

    public MyTableLayout(Context context) { 
     super(context); 
    } 

    // indexListButtons is the current index of the listButtons elements. so  as long as there are buttons, we will add them to the table rows 
    int indexListButtons = 0; 
    public void createTableLayoutOfButtons(int numRows, int numCols,  List<GifImageButton> listButtons) { 
     setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT)); 

     for (int i = 0; i < numRows; ++i) { 
      TableRow tableRow = new TableRow(getContext()); 
      LinearLayout newLinearLayout = new LinearLayout(getContext()); 
      newLinearLayout.setLayoutParams(new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f)); 
      newLinearLayout.setOrientation(LinearLayout.VERTICAL); 
      tableRow.addView(newLinearLayout, new TableLayout.LayoutParams(
        TableLayout.LayoutParams.MATCH_PARENT, 
        TableLayout.LayoutParams.MATCH_PARENT, 1.0f)); 
      tableRow.setGravity(Gravity.CENTER); 

      for (int j = 0; j < numCols; ++j, ++indexListButtons) { 
       // indices 0, 1, 2, 3 
       if (indexListButtons < listButtons.size()) { 
        newLinearLayout.addView(listButtons.get(indexListButtons)); 
       } 
       // indices bigger than 3 don't exist so insert empty views  in order to make each of the views in the same size 
       else { 
        // not enough buttons 
        TableRow.LayoutParams params = new  TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1.0f); 
        Button btn = new Button(getContext()); 
        btn.setLayoutParams(params); 
        btn.setVisibility(View.INVISIBLE); 
        newLinearLayout.addView(btn); 
       } 
      } 
      addView(tableRow); 
     } 
    } 
} 
+0

テーブルの行の高さに重さを設定することで、これはボタンの幅と同じように行うことができます。このようにしてテーブルの行を分割すると、残りの空き領域が分割され、すべての垂直領域を満たす高さの行が得られます。 –

+0

tableRow.setLayoutParams(新しいTableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT、 0,1.0f)); このような意味ですか? \ –

+0

そうですね、そうですね、TableLayoutのドキュメントによれば、子がTableRowの場合、高さは常にWRAP_CONTENTなので、体重が助けにならないと言います。これを修正するには2つの方法があります。 - ビューサイズを自分で計算することもできます。ポスト実行可能ファイルでこれを行うので、親レイアウトが既に高さを測定している次のフレームにサイズが設定されます。次に、ボタンの高さを親の高さの半分に設定するだけで、テーブルの行のサイズが正しく変更されます。 - もう1つの方法は、縦のLinearLayoutを、2つの子LinearLayoutsとその重みを設定して使用することです。 –

答えて

1

あなたはこのような何かをすることによって期待される結果を得ることができます:あなたはより多くの行が必要な場合は、あなたのループで「currentRowの」レイアウトを作成するコードをラップすることができます

LinearLayout main = new LinearLayout(context); 
    main.setOrientation(LinearLayout.VERTICAL); 

    // for(etc. etc.) 
    LinearLayout currentRow = new LinearLayout(context); 
    currentRow.setOrientation(LinearLayout.HORIZONTAL); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, 0); 
    params.weight = 1.0f; 
    currentRow.setLayoutParams(params); 

    View viewOne = new View(context); 
    params = new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT); 
    viewOne.setLayoutParams(params); 
    currentRow.addView(viewOne); 

    View viewTwo = new View(context); 
    params = new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT); 
    viewTwo.setLayoutParams(params); 
    currentRow.addView(viewTwo); 

    main.addView(currentRow); 
    //} end of for 

。この方法では、テーブルレイアウトを使用する必要がなく、比較的簡単に作成できます。欠点は、ネストされたレイアウトを使用するので問題はありませんので、効率的ではありません。

10以上のような多くの行が必要な場合は、おそらくRecyclerViewをGridLayoutManagerとともに使用して、あまりにも多くのビューと非常に重いレイアウトを作成しないようにする必要があります。

+0

ありがとうございました!!!私は何時間もそれを修正しようとしました! :) 私はあなたの助けに感謝します!それはうまくいった!ビューのparamsは3番目のパラメータとして1.0fを取得する必要があります。params = new LinearLayout.LayoutParams(MATCH_PARENT、MATCH_PARENT、1.0f); がないと、各行に1つの要素が含まれているためです。 もう一度ありがとう! –

+1

@ErezShmielそうですね、私はビュー自体に重みを加えるのを忘れていました。うれしいことはあなたの問題を解決:) –

関連する問題