动态地在垂直和水平的方式添加视图视图、水平、方式、动态

2023-09-07 01:57:23 作者:陪你ら不孤单゛

我试图创建的LinearLayout 按钮动态,我想添加这些垂直和水平的方法。

I'm trying to create Buttons in LinearLayout dynamically, and I want to add those in vertical and horizontal way.

首先,在布局中添加按钮A ,以及是否有按钮A 和屏幕之间有足够的空间边,按钮B 添加到按钮A (水平)的权利。否则,添加按钮B 下面按钮A (垂直)。

At first, add a button A in the layout, and if there's enough space between button A and screen edge, add button B to the right of button A (horizontally). Otherwise, add button B below button A (vertically).

我目前的布局:

<LinearLayout
    android:id="@+id/btn_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
</LinearLayout>

类:

LinearLayout btnLayout = (LinearLayout) findViewById(R.id.btn_layout);
btnLayout.removeAllViewsInLayout();
for(Tag tag : tagList.getChildTags()) {
    Button button = new Button(this);
    button.setId(tag.getId());
    button.setText(tag.getName());
    btnLayout.addView(button);
}

在这种情况下,如果设置了定位横向,然后一些按钮都没有显示(cut-关闭的屏幕),如果我设置为垂直,它看起来pretty坏。

In this case, if I set orientation as horizontal, then some of buttons are not showing (cut-off by screen), and if I set as vertical, it looks pretty bad.

有什么办法来解决这个问题?谢谢大家提前!

Is there any way to solve this problem? Thanks everyone in advance!

推荐答案

可以做到这一点,但不是在一个平凡的路。我将解释我是怎么做类似的TableRow 什么东西(在我的情况,我想补充的TextView S),如果它们适合。

You can achieve this but not in a trivial way. I'll explain how I do something similar (in my case, I add TextViews) to TableRows, if they fit.

通过这种方法,你将不得不使用一个 TableLayout 并添加的TableRow s到它与你的按钮秒。所以,你可能会取代你的@ + ID / btn_layout 的LinearLayout 是一个 TableLayout 代替。

With this approach you'll have to use a TableLayout and add TableRows to it with your Buttons. So you might replace your "@+id/btn_layout" LinearLayout to be a TableLayout instead.

首先,让屏幕的宽度,使用这样的:

Firstly, to get the screen's width, use something like this:

final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);

final WindowManager.LayoutParams params = getWindow().getAttributes();

// Your screen's width will be stored within your params.width value    

您将使用该知道,如果电流按钮还是适合中屏幕的宽度当前的TableRow 或它被添加到一个新的。所以,现在,使用这样的创建按钮:

You'll use this to know if the current Button still fits the screen's width within the current TableRow or it has to be added to a new one. So now, use something like this to create your buttons:

int currentRowsWidth = 0;
TableLayout tl = (TableLayout) findViewById(R.id.my_table_layout);
TableRow currentRow = new TableRow();

for (Tag tag : tagList.getChildTags()) {
  Button button = new Button(this);
  button.setId(tag.getId());
  button.setText(tag.getName());

  // There's where you check whether it still fits the current `TableRow` or not
  if (currentRowsWidth + button.getWidth() < params.width) {
    currentRowsWidth += button.getWidth();
    currentRow.addView(button);
  }
  else {
    // It doesn't fit, add the currentRow to the table and start a new one
    tl.add(currentRow);
    currentRow = new TableRow();
    currentRow.addView(button);
    currentRowsWidth = button.getWidth();
  }
}

这可能发生,一旦你走出循环的还有按钮 s到中添加 currentView ,简单地测试了:

It might happen that once you get out of the loop there are still Buttons to add in the currentView, simply test it:

if (currentRow.getChildCound() > 0)
  tl.add(currentRow);

我从头写这个,所以有些事情可能不是第一次编译,但我希望你的想法。

I'm writing this from head, so some things might not compile at first time, but I hope you get the idea.