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

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

问题描述

我建立了从code一个用户界面,并可以成功添加进度窗口小部件,但是我不能改变窗口小部件来,我需要值的尺寸,它总是停留在默认大小(约50dp)。我试过下面的code;

 进度进度=新进度(活动,空,android.R.attr.progressBarStyleHorizo​​ntal);
    progressBar.setMinimumHeight(20);
    progressBar.setMinimumWidth(100);
 

我也试着设置进度的LayoutParams却又没什么,我已经试过包装的小部件在另一个布局,但仍然没有运气。

谁能帮助,因为我看不到怎么办呢?如果使用XML它可以工作一个解决方案,但我必须在运行时设置的尺寸和位置。


有关进展XML我用下面的。

 <进度
      的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =10dp
      机器人:indeterminateOnly =假
      机器人:progressDrawable =@机器人:可绘制/ progress_horizo​​ntal
      机器人:indeterminateDrawable =@机器人:可绘制/ progress_indeterminate_horizo​​ntal
      机器人:=了maxWidth1000dp
   />
 

那么code膨胀其计算方法如下。这就是我们一直在讨论的混合。

  LayoutInflater layoutInflater =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    进度进度=(进度)layoutInflater.inflate(R.layout.progress_bar,NULL);
    progressBar.setMinimumHeight(10);
    progressBar.setMinimumWidth(200);
    progressBar.setLayoutParams(新RelativeLayout.LayoutParams(200,10);

    progressBar.setMax(100);
    progressBar.setProgress(getScrollBarValue());
    progressBar.setIndeterminate(isIndeterminate());
    。progressBar.getLayoutParams()宽度= 200;
    progressBar.invalidate();
 

解决方案

R.layout.activity包括这样的:

 <进度
        机器人:ID =@ + ID /进步
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:不确定=真
        机器人:能见度=看见
        风格=@安卓风格/ Widget.ProgressBar.Large.Inverse
/>
 

然后是code:

 公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity);
    进度栏=(进度)findViewById(R.id.progress);
    。bar.getLayoutParams()高度= 500;
    bar.invalidate();
}
 

到无效()方法的调用是很重要的,因为这是当视图重绘采取变化考虑进去。虽然直观,但它允许通过配料的变化更好的性能。

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);

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.

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.


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 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"
/>

And then the code:

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();
}

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.

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

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