<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 背景图 -->
<item android:id="@android:id/background"
  android:drawable="@drawable/back" />
<!-- 进和能量图 -->
<item android:id="@android:id/progress" 
      android:drawable="@drawable/fill" />
</layer-list>我在seekbar中定义了前景图和背景图。seekbar现在能按我的要求显示前景和背景。我现在的问题是如何估代码中动态的替换前景图和背景图。假如图片比较多。如果10张前景,10张背景就要做10*10个xml。这个现实中比较麻烦。

解决方案 »

  1.   

    O了。我用的是这个代码.
      Resources res = getResources();
            
            LayerDrawable progressDrawable =    (LayerDrawable) res.getDrawable(R.drawable.seekbar_progress);
         progressDrawable.setDrawableByLayerId(android.R.id.background, res.getDrawable(R.drawable.back));
        
         ClipDrawable proDrawable = new ClipDrawable( res.getDrawable(R.drawable.fill1), Gravity.LEFT,ClipDrawable.HORIZONTAL);
         progressDrawable.setDrawableByLayerId(android.R.id.progress,proDrawable);            seekBar1.setProgressDrawable(progressDrawable);
    长大见识了。
      

  2.   

    这样一来,按钮,文本加了xml的看来都可以用这个方法来改变了。
      

  3.   

    seekbar_progress.xml, 把它放在/res/drawable/ folder:
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <nine-patch
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:src="@drawable/seekbar_background"
            android:dither="true"
         />
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <gradient
                    android:startColor="#80028ac8"
                    android:centerColor="#80127fb1"
                    android:centerY="0.75"
                    android:endColor="#a004638f"
                    android:angle="270"
                />
            </shape>
        </clip>
    </item>
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/seekbar_progress_bg"
    />
    </layer-list>
    SeekBar
    <SeekBar
        android:id="@+id/frequency_slider"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="20"
        android:progress="0"
        android:secondaryProgress="0"
        android:progressDrawable="@drawable/seekbar_progress"
        android:thumb="@drawable/seek_thumb"
     />Programmatically
    // ------------ custom seekbar
    LayerDrawable layer = (LayerDrawable) verticalSeekBar_1.getProgressDrawable();Drawable draw1 = (Drawable)layer.findDrawableByLayerId(android.R.id.progress);
    Bitmap bitmap1 = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.scroll_on);draw1 = new ClipDrawable(new BitmapDrawable(bitmap1), Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);layer.setDrawableByLayerId(android.R.id.progress, draw1);Drawable draw2 = (Drawable) layer.findDrawableByLayerId(android.R.id.background);Bitmap bitmap2 = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.scroll_off);draw2 = new BitmapDrawable(bitmap2);
    layer.setDrawableByLayerId(android.R.id.background, draw2);你也可以参考:http://www.mokasocial.com/2011/02/create-a-custom-styled-ui-slider-seekbar-in-android/