插入图像到桌面视图不覆盖整个屏幕

问题描述:

enter image description here 当我的图像的分辨率为128x128并插入到tableview(应该覆盖整个屏幕)时,它们不覆盖整个屏幕,但是当我将它们调整为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); 
     } 
    } 
} 

======

编辑

======

韦塞林·托多罗夫,让我明白,如果这是你的意思。感谢..

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,所以体重无济于事。我看到了两种解决方法: - 您可以自己计算视图大小。在可运行的文章中这样做,以便尺寸在您的父版面已经测量其高度的下一帧上设置。然后,您可以将按钮高度设置为父高度的一半,并且表格行将被正确调整大小。 - 另一种方法是使用垂直LinearLayout和两个子LinearLayouts并设置其权重。 –

你可以做这样的事情得到预期的结果:

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 

你可以用在你的循环创造了“currentRow”布局,如​​果你需要更多的行的代码。这样你就不必使用表格布局,而且它的创建也相对简单。缺点是效率不高,因为它使用嵌套布局,但不应该是个问题。

另一个要考虑的是,如果你需要多行像10或以上,你应该使用一个RecyclerView与GridLayoutManager这样你就不会产生太多的意见和一个非常沉重的布局。

+0

非常感谢!我试图修复这么多小时! :) 我感谢您的帮助!它运作良好!只是一个编辑:视图的params应该得到1.0f作为第三个参数:params = new LinearLayout.LayoutParams(MATCH_PARENT,MATCH_PARENT,1.0f); 因为没有它,每行包含1个元素。 再次感谢! –

+1

@ErezShmaiel是的,你是对的,我忘了给视图本身增加重量。很高兴它解决了你的问题:) –