我有与activity1关联的layout.xml,它有各种组件,包括linearlayout,我还在progress.xml中存储了一个进度条,在运行时使用LayoutInflater我正在检索进度条并将其添加到layout.xml的linearlayout
我的progress.xml:
案例1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="100dp"
android:layout_height="102dp"
android:layout_gravity="center"
android:indeterminate="false"
android:max="100"
android:progress="100"
android:progressDrawable="@drawable/custom_bar"
/>
</LinearLayout>
案例2:
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="100dp"
android:layout_height="102dp"
android:layout_gravity="center"
android:indeterminate="false"
android:max="100"
android:progress="100"
android:progressDrawable="@drawable/custom_bar"
/>
当我在case1中使用xml时,我可以在运行时成功添加进度条,但是当我使用case2 xml时,我无法在运行时添加进度条...请帮助..
我正在使用以下代码从case2 xml添加进度条:
LinearLayout ll = (LinearLayout) findViewById(R.id.container_linear_layout);
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View otherView = layoutInflater.inflate(R.layout.progress, null);
pb = (ProgressBar) otherView.findViewById(R.id.progressBar1);
ll.addView(pb);