简体   繁体   English

如何更改进度条的大小(宽度和高度)以适合所有线性布局?

[英]How to change the size (width and height) of progressbar to fit all in linearlayout?

I would like to add an unknown amount of progressbar to a linearlayout.我想向线性布局添加未知数量的进度条。 (Like Whatsapp Status, and Instagram Stories) I can add progressbar to the linearlayout but the progressbar just shows max 7 progressbar. (如 Whatsapp 状态和 Instagram 故事)我可以将进度条添加到线性布局,但进度条只显示最多 7 个进度条。 带有 7 个进度条的进度条,即使应该是 10

My code so far:到目前为止我的代码:

while (i < 10){
    i ++;
    View child = getLayoutInflater().inflate(R.layout.stories_progress, null);
    linearLayout.addView(child);
}

I would like to shrink the size of the progressbar programmatically, so when I have 2 progressbar, they fill the layout with 50%/50%, when 5, 10 or even 20 progressbar, they become smaller, all of them will be displayed on linearlayout I tried to get the current width of the linearlayout and divide by the number of progressbars, but the width of the linearlayout returns always 0;我想以编程方式缩小进度条的大小,所以当我有 2 个进度条时,它们会以 50%/50% 填充布局,当 5、10 甚至 20 个进度条时,它们会变小,它们都会显示在线性布局我试图获取线性布局的当前宽度并除以进度条的数量,但线性布局的宽度总是返回0;

My xml:我的 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout_id"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:orientation="horizontal"
    tools:context=".stories">

</LinearLayout>

My progressbar我的进度条

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">

    <ProgressBar
        android:id="@+id/stories_progressBar_id"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ProgressBar>

</LinearLayout>

It is likely that you are not waiting for the top LinearLayout to have a size.您可能没有等待顶部的LinearLayout具有大小。 You can wait for a size to be assigned by using a ViewTreeObserver.OnGlobalLayoutListener .您可以等待使用ViewTreeObserver.OnGlobalLayoutListener分配大小。 Here is some code that should help:这是一些应该有帮助的代码:

linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);

        linearLayout.removeAllViews();
        int newWidth = linearLayout.getWidth() / 10;
        int newHeight = linearLayout.getHeight();
        int i = 0;
        while (i < 10) {
            i++;
            LinearLayout child =
                    (LinearLayout) getLayoutInflater().inflate(R.layout.stories_progress,
                            linearLayout, false);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
            lp.width = newWidth;
            lp.height = newHeight;
            linearLayout.addView(child);
        }
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM