当我在 xml 中设置值时,我的进度条不起作用。
首先,当我以编程方式创建实例时:
LinearLayout progressBarLayout = new LinearLayout(activity);
progressBarLayout.setOrientation(LinearLayout.HORIZONTAL);
ProgressBar progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
progressBar.setBackgroundDrawable(activity.getResources().getDrawable(android.R.drawable.progress_horizontal));
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
ShapeDrawable pgDrawable = new ShapeDrawable(new RectShape());
pgDrawable.getPaint().setColor(Color.parseColor("yellow"));
ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
progressBar.setProgressDrawable(progress);
progressBar.setProgress(activity.getModel().getProgress());
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
progressBar.setLayoutParams(params);
progressBarLayout.addView(progressBar);
bodyLayout.addView(progressBarLayout);
但是当我使用 xml 时:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" android:orientation="vertical" android:padding="10dp" > <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/progressBar" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxHeight="30dp" android:minHeight="30dp" android:progressDrawable="@drawable/progressbar_drawable"> </ProgressBar> </LinearLayout>
可绘制:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item
android:id="@android:id/progress"
>
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
JAVA代码:
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setProgress(activity.getModel().getProgress());
bodyLayout.addView(progressBarLayout);
这给了我一个例外(黑屏),我不知道为什么。java中的代码在Viewer类中。我有一个活动控制器实例查看器:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("innnnnnnnnnnnnnnnnnnnnfo", "menu");
try {
setContentView(R.layout.progress_layout);
detector = new SimpleGestureFilter(this, this);
this.mUser = (MaritacaUser) getIntent().getSerializableExtra(Constants.USER_DATA);
if (mUser != null) {
InputStream file = getResources().getAssets().open(Constants.FORM_FILENAME);
setModel(new Model(file));
if (!getModel().isQuestionsEmpty()) {
viewer = new Viewer(ControllerActivity.this, getModel()
.getCurrentQuestion());
}
setContentView(viewer.getView());
}
} catch (Exception e) {
throw new MaritacaException(e.getMessage());
}
}
我认为(ProgressBar) findViewById(R.id.progressBar); 正在返回 null,但在 R 类中显示 ProgressBar。我在此代码上找不到错误,也许我必须在活动中而不是在查看器中实例化 ProgressBar?