始终显示3个按钮的Horizo​​ntalScrollView按钮、Horizo、ntalScrollView

2023-09-06 17:54:03 作者:ゐ尔守侯

大家好,我想显示3至X按钮上的机器人。 我们的想法是要始终从3个按键,是以屏幕(宽)大小的各33%,并能够滚动的 Horizo​​ntaly 通过的项目。

Hello everyone I am trying to display 3 to X buttons on android. The idea is to always start with 3 buttons that takes each 33% of the size of the screen (width) And to be able to scroll Horizontaly through items.

此外这些商品中,将编程添加到视图。

Also theses items will be added programmatically to the view.

我试图把一个LinearLaout水平的Horizo​​ntalScrollView之内。 然后的addChild到的LinearLayout。但项调整大小和它不会滚动。

I tried to put a LinearLaout horizontal within an HorizontalScrollView. And then addchild to the linearlayout. but the items resize and it doesn't scroll.

这是不是正确的做法?或有没有人有一个想法如何使它?

Is it the right approach ? or does anyone have an idea how to make it ?

Class.java

Class.java

HomeCircledButton button = HomeCircledButton_.build(this);
button.title.setText(sc.get(i).getLabel());
LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 0.33f));
homeButtonsLL.addView(button);

Layout.xml

Layout.xml

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:gravity="center"
                android:weightSum="1.0"
                android:id="@+id/home_buttons_ll">
           </LinearLayout>
  </HorizontalScrollView>

我也试图在XML已经创建的按钮和隐藏它们编程(View.GONE),但他们只是调整

I also tried to create the buttons already in the XML and hide them programatically (View.GONE) but they just resize

推荐答案

如果您要设置动态的大小,有数目不详的孩子的意见,然后重量办法ISN 'T是可行的。代替获得屏幕的宽度,并根据该设置按钮的宽度。也, LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.33f)); 正如你所期望此行的code将无法正常工作。你需要把params为按钮,使其工作。

If you are setting the size dynamically and there are unknown number of child views, then weight approach isn't that feasible. Instead get the width of the screen, and based on that set the width of the button. Also, LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 0.33f)); this line in your code will not work as you expected. You need to set the params to button to make it work.

您可以尝试这样的事情,

You can try something like this,

HomeCircledButton button = HomeCircledButton_.build(this);
button.title.setText(sc.get(i).getLabel());
//divide the screen width by 3
int buttonWidth = getScreenWidth() / 3;
LinearLayout.LayoutParams buttonparams= new LinearLayout.LayoutParams(buttonWidth, LinearLayout.LayoutParams.MATCH_PARENT);
button.setLayoutParams(buttonparams);
homeButtonsLL.addView(button);

...

private int getScreenWidth( ) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        int width;
        getWindowManager().getDefaultDisplay()
        .getMetrics(displayMetrics);
        width = displayMetrics.widthPixels;
        return width;
    }

和你没有设置权重之和在XML中,

and you don't have to set weight sum in xml,

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:gravity="center"
                android:id="@+id/home_buttons_ll">
           </LinearLayout>
  </HorizontalScrollView>