java - 将图像插入表格 View 不会覆盖整个屏幕

标签 java android tableview image-resizing screen-resolution

enter image description here 当我的图像分辨率为 128x128 并插入到 tableview(应该覆盖整个屏幕)时,它们不会覆盖整个屏幕,但是当我将它们调整为 256x256 时,它们会覆盖整个屏幕。我不能使用 9 补丁图像或其他东西来使它们覆盖整个屏幕并在任何屏幕分辨率下都按需要运行吗?我必须使用密度吗?

我尝试将 fitXY 设置为我的 View ,但它没有解决我的问题。

我不确定是否需要,但这是我的代码:

我创建了 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);
}

然后我以编程方式创建了一个表格布局(行 = 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);
        }
    }
}

======

编辑

======

Veselin Todorov,让我明白这是不是你的意思。谢谢..

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);
        }
    }
}

最佳答案

您可以通过执行以下操作获得预期的结果:

    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 行或更多行,您应该使用带有 GridLayoutManager 的 RecyclerView,这样您就不会创建太多 View 和非常繁重的布局。

关于java - 将图像插入表格 View 不会覆盖整个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40628803/

相关文章:

java - 为什么我对位于/data/user_de/中的文件的权限被拒绝

java - XML schema 可以在单个 complexType 中有多个选择?

android - fragment 2 fragment 通信

java - 如何在不完成/停止子 Activity 的情况下完成子 Activity 的父 Activity ?

sorting - JavaFX 按日期对 TableView 列进行排序(dd/mm/yyyy 格式)

javafx-2 - 如何使JavaFX TableView单元格可编辑?

java - Eclipse 声称我没有名为 R.id.list 的 ListView

java - 如何在 Play 中设置默认 PostgreSQL schema?

java - 如何在使用输入/输出流和 Android 11 中已弃用的处理程序异步任务复制文件时更新 ProgressBarDialog

java - 使用 VBox JavaFX-8 内的过滤器参数过滤 TableView