我尝试通过xml制作自定义搜索栏。但是在第一次触摸之前只显示拇指。触摸后,会显示自定义搜索栏。
这是我在mainActivity中的xml。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="129dp"
>
<!--seek bar-->
<SeekBar
android:layout_width="match_parent"
android:layout_height="40dp"
android:thumb="@mipmap/survey_bar_ic"
android:thumbOffset="16dp"
android:id="@+id/seekBar"
android:layout_marginLeft="23dp"
android:layout_marginRight="23dp"
android:layout_centerInParent="true">
</SeekBar>
<RelativeLayout
android:layout_width="37dp"
android:layout_height="28dp"
android:layout_above="@+id/seekBar"
android:layout_marginBottom="8dp"
android:id="@+id/bubble"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bubble_img"
android:background="@mipmap/survey_bubble_green"></ImageView>
<TextView
android:id="@+id/bubble_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1"
android:textColor="#ffffff"
android:layout_centerInParent="true"
android:gravity="center"
></TextView>
</RelativeLayout>
</RelativeLayout>
这是survey_bar_free.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--basic background-->
<item android:id="@android:id/background">
<shape android:shape="line">
<stroke android:width="16dp" android:color="#dcdcdc"/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape android:shape="line">
<stroke android:width="16dp" android:color="#8ed351"/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape android:shape="line">
<stroke android:width="16dp" android:color="#8ed351"/>
</shape>
</clip>
</item>
</layer-list>
这是mainActivity.java
seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener
{
if(i>free_range)
{
if(i>point_range)
{
int cur = (i-point_range)/2+point_num;
bubbleText.setText(Integer.toString(cur));
seekBar.setProgressDrawable(
getDrawable(R.drawable.survey_bar_premium));
int temp = i/2;
seekBar.setProgress(temp*2);
//seekBar.setProgress(cur*2);
bubbleImg.setImageResource(R.mipmap.survey_bubble_blue);
}
else
{
int cur = i/6;
bubbleText.setText(Integer.toString(cur));
seekBar.setProgressDrawable(
getDrawable(R.drawable.survey_bar_point));
seekBar.setProgress(cur*6);
bubbleImg.setImageResource(R.mipmap.survey_bubble_skyblue);
}
}
else
{
int cur = i/6;
if(cur==0)
{
cur=1;
}
bubbleText.setText(Integer.toString(cur));
seekBar.setProgressDrawable(
getDrawable(R.drawable.survey_bar_free));
seekBar.setProgress(cur*6);
bubbleImg.setImageResource(R.mipmap.survey_bubble_green);
}
});
seekBar.setMax(seekBarMax);
seekBar.setProgress(6);
seekBar.setProgressDrawable(
getDrawable(R.drawable.survey_bar_free));