当我的图像分辨率为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(rows = 2,columns = 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);
}
}
}
答案 0 :(得分:1)
您可以通过以下方式获得预期结果:
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,这样你就不会创建太多的视图和非常繁重的布局。