更改在运行时添加的 ProgressBar 的宽度 [英] Change the width of a ProgressBar added at runtime

查看:31
本文介绍了更改在运行时添加的 ProgressBar 的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从代码构建 UI 并且可以成功添加 ProgressBar 小部件,但是我无法将小部件的尺寸更改为我需要的值,它始终保持默认大小(大约 50dp).我试过下面的代码;

I am building up a UI from code and can successfully add ProgressBar widgets, however I cannot alter the dimensions of the widget to values I need, it always stays at the default size (around 50dp). I've tried the following code;

    ProgressBar progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
    progressBar.setMinimumHeight(20);
    progressBar.setMinimumWidth(100);

我也尝试过设置 progressBar LayoutParams,但同样没有设置,我尝试将小部件包装在另一个布局中,但仍然没有运气.

I've also tried setting the progressBar LayoutParams but again nothing and I've tried wrapping the widget in another layout, but still not luck.

任何人都可以帮忙,因为我不知道该怎么做?如果有使用 XML 的解决方案,它可以工作,但我必须在运行时设置维度和位置.

Can anyone help, as I can't see how to do it? If there is a solution using XML it could work, but I have to set the dimension and location at runtime.

对于 Progress XML,我使用了以下内容.

For Progress XML I used the following.

<ProgressBar
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent"
      android:layout_height="10dp"
      android:indeterminateOnly="false"
      android:progressDrawable="@android:drawable/progress_horizontal"
      android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
      android:maxWidth="1000dp"
   />

然后给它充气的代码如下.这是我们一直在讨论的混合内容.

Then the code to inflate it is as follows. This is a mix of what we have been discussing.

    LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ProgressBar progressBar = (ProgressBar)layoutInflater.inflate(R.layout.progress_bar, null);
    progressBar.setMinimumHeight(10);
    progressBar.setMinimumWidth(200);
    progressBar.setLayoutParams(new RelativeLayout.LayoutParams(200, 10);

    progressBar.setMax(100);
    progressBar.setProgress(getScrollBarValue());
    progressBar.setIndeterminate(isIndeterminate());
    progressBar.getLayoutParams().width = 200;
    progressBar.invalidate();

推荐答案

R.layout.activity 包括:

R.layout.activity includes this:

<ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:visibility="visible"
        style="@android:style/Widget.ProgressBar.Large.Inverse"
/>

然后是代码:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    ProgressBar bar = (ProgressBar)findViewById(R.id.progress);
    bar.getLayoutParams().height = 500;
    bar.invalidate();
}

对 invalidate() 方法的调用很重要,因为当重新绘制视图以考虑更改时.虽然不直观,但它确实可以通过批量更改获得更好的性能.

The call to the invalidate() method is important as that is when the view is redrawn to take the changes into account. Although unintuitive, it does allow for better performance by batching changes.

这篇关于更改在运行时添加的 ProgressBar 的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆