检索动态膨胀布局以使用

时间:2011-02-02 07:34:21

标签: android layout android-linearlayout

我正在尝试从xml文件中动态地将一些ProgressBar视图添加到膨胀的布局中。运行以下代码时,我继续获取ClassCastExceptions:

     // === This is the part I'm having trouble with ===
     ProgressBar v = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, ll);
     ProgressBar p = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, ll);

下面的代码是如何在没有强制转换的情况下运行和运行,但是我需要将视图作为进度条来处理。有没有办法在没有得到ClassCastException的情况下做到这一点

    /**
     * Pulls the layout from R.layout.listview and creates a single list
     * entry and returns it as a view to be put into the listview
     */
     public View getView(int position, View convertView, ViewGroup parent) {
        // the view to be returned
        View itemLayoutView = convertView;

        // if the view doesn't exist, create the layout from the inflator
        if (itemLayoutView == null) {
            LayoutInflater mLayoutInflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // use listview.xml
            itemLayoutView = mLayoutInflater.inflate(R.layout.listview,
                    null);

            LinearLayout ll = (LinearLayout) itemLayoutView.findViewById(R.id.List_Main_LinearLayout_ProgressBars);

            // === This is the part I'm having trouble with ===
            View v = mLayoutInflater.inflate(R.layout.dayprogressbar, ll);
            View p = mLayoutInflater.inflate(R.layout.dayprogressbar, ll);


        }

这是我正在膨胀的R.layout.dayprogressbar.xml:

<ProgressBar
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:progress="50"
    android:indeterminateOnly="false"
    android:progressDrawable="@android:drawable/progress_horizontal"
    android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
    android:minHeight="1dip"
    android:maxHeight="1dip"
    android:max="100"
    android:paddingLeft="1dp"
    android:layout_weight="2"
/>

1 个答案:

答案 0 :(得分:0)

没试过这些但是,无论如何我会发布它们。 :)

我有两个意见:

1)因为您的R.layout.dayprogressbar.xml是ProgressBar,所以将v和p转换为进度条

ProgressBar v = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, ll);
ProgressBar p = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, ll);

我不知道你是否可以同时膨胀两个观点..最近没有完成我的作业.lol

2)您可以对这些视图进行充气,然后将其添加到ll

ProgressBar v = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, null);
ProgressBar p = (ProgressBar) mLayoutInflater.inflate(R.layout.dayprogressbar, null);
ll.addView(v);
ll.addView(p);

你可能需要做一些调整,但至少我已经给你一些可能的解决方案。

希望至少有一个适合你..