我丢失了ProgressBar将我的布局移动到自定义组件

时间:2014-03-31 08:53:55

标签: android android-layout android-custom-view android-drawable android-progressbar

我有一个“复杂”的布局,我想“移动”到自定义组件,以便能够轻松地重新利用,但当我这样做时,ProgressBar消失了。我认为原因可能是ProgressBar有自定义progressDrawable。

在代码执行期间,进度条是可访问且可见的(我无法看到它,但我检查了visibility属性不是问题......)并且没有错误。

我的自定义布局

        ... 
        </LinearLayout>

    </LinearLayout>

    <!-- Progressbar -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        >
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="5dp"
            android:id="@+id/progressBar"
            android:progressDrawable="@drawable/customprogressbar"
            android:layout_gravity="center_horizontal"

            />
    </LinearLayout>

</LinearLayout>

我的自定义布局类

public class CustomLayout extends LinearLayout {
    public CustomLayout(Context context) {
        super(context);

    }
    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.custom_layout, this, true);
    }
}

我的progressDrawable

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Define the background properties like color etc -->
    <item android:id="@android:id/background">
        <shape>
            <gradient
                android:startColor="#000001"
                android:centerColor="#0b131e"
                android:centerY="1.0"
                android:endColor="#0d1522"
                android:angle="270"
                />
        </shape>
    </item>

    <!-- Define the progress properties like start color, end color etc -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:startColor="#007A00"
                    android:centerColor="#007A00"
                    android:centerY="1.0"
                    android:endColor="#06101d"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>
</layer-list>

我的容器布局主要活动

<?xml version="1.0" encoding="utf-8"?>

<my.custom.namespace xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent">

</my.custom.namespace>

我的主要活动

...

setContentView(R.layout.main_activity);

...

mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

// Progress bar is get fine, and I checked that visibility is VISIBLE
//   I didn't change a line of code of this after moving the layout to a custom control
String showProgressbar = sharedPrefs.getString("settings_progressbar_value", Constants.PROGRESSBAR_VISIBLE_BY_DEFAULT);
if (showProgressbar.equals(Constants.PROGRESSBAR_VISIBLE_BY_DEFAULT)) {
    mProgressBar.setVisibility(View.VISIBLE);
} else {
    mProgressBar.setVisibility(View.GONE);
}

0 个答案:

没有答案