Когда мои изображения имеют разрешение 128x128 и вставлены в табличное представление (которое должно охватывать весь экран), они не покрывают весь экран, но когда Я изменяю их размер до 256х256, затем они охватывают весь экран. Могу ли я использовать 9 патч-изображений или что-то еще, чтобы заставить их покрывать весь экран и вести себя по мере необходимости в любом разрешении экрана? Должен ли я использовать плотность?Вставка изображений в настольный вид не распространяется на весь экран
Я попытался установить fitXY в мои взгляды, но это не решило мою проблему.
Я не уверен, если это необходимо, но вот мой код:
Я создал 4 кнопки рисунок и добавил их в список кнопок изображения 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
======
Веселин Тодоров, дайте мне понять, если это был ваш смысл. спасибо ..
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);
}
}
}
Я думаю, вы можете достичь этого, установив вес для высоты стола так же, как вы это делаете, для ширины кнопок. Таким образом, таблицы строк shoul разделяют оставшееся свободное пространство между собой, и вы получите строки равной высоты, которые заполняют все вертикальное пространство. –
tableRow.setLayoutParams (новый TableLayout.LayoutParams ( TableLayout.LayoutParams.MATCH_PARENT, 0, 1.0f)); Вы имели в виду вот так? он ничего не меняет: \ –
Да, вы правы, в документации TableLayout говорится, что если ребенок является TableRow, высота всегда WRAP_CONTENT, поэтому вес не поможет. Я вижу два способа исправить это: - Вы можете самостоятельно вычислить размеры размеров. Сделайте это в post runnable, чтобы размер был установлен в следующем кадре, где ваш родительский макет уже измерил его высоту. Затем вы можете просто установить высоту кнопок на половину родительской высоты, и строки таблицы будут правильно изменены. - Другим способом является использование вертикального LinearLayout с двумя дочерними LinearLayouts с установленными весами. –