gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:53:01 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com