17_Android效果:SeekBar的简单使用

Android基础汇总一.    自定义seekBar样

ssq

 

#1 定义seekBar背景图片:    
 
<?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/user_management_myseekbar_background">
    </item>
     <item
        android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/user_management_myseekbar_background">
    </item>
<!-- 设置第二进度:一般视频的缓存效果-->
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/user_management_myseekbar_progress">
    </item>

</layer-list>
 
#2 使用
 <SeekBar
                    android:layout_gravity="center_horizontal"
                    android:id="@+id/seekBar"
                     android:layout_width="284dp" 设置宽度最好根据背景图片一点点调
                    android:layout_height="wrap_content"
                    android:max="100"
                    android:maxHeight="16dp"
                    android:minHeight="16dp"
                     android:thumbOffset="0dp" 默认滑块中间从进度条开始滑动,设置为0dp则滑块最左边从进度条开始滑动
                    android:progress="0"
                     android:progressDrawable="@drawable/seekbar_style"  设置背景图片和进度的图片
                     android:thumb="@drawable/my_seekbar_thumb" /> 设置滑块的图片
 
二、 SeekBar带进度显示
ssq
# 1图片:
my_seekbar_thumb.png
ssq
user_management_myseekbar_background.png
ssq
user_management_myseekbar_progress.png
ssq
#2 xml:
        seekbar_style.xml
    <?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/user_management_myseekbar_background">
 
    </item>
 
 
    <item
 
        android:id="@android:id/progress"
 
        android:drawable="@drawable/user_management_myseekbar_progress">
 
    </item>
 
</layer-list>
 
fragment_main.xml:
        <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
    xmlns:tools=" http://schemas.android.com/tools"
 
    android:layout_width="match_parent"
 
    android:layout_height="match_parent"
 
    android:orientation="vertical"
 
    android:paddingBottom="@dimen/activity_vertical_margin"
 
    android:paddingLeft="@dimen/activity_horizontal_margin"
 
    android:paddingRight="@dimen/activity_horizontal_margin"
 
    android:paddingTop="@dimen/activity_vertical_margin"
 
    tools:context="com.bpj.setlevel.MainActivity$PlaceholderFragment" >
 
 
    <TextView
 
        android:layout_marginLeft="26dp"
 
        android:id="@+id/tv_hint"
 
        android:layout_width="wrap_content"
 
        android:layout_height="wrap_content"
 
        android:text="0" />
 
 
    <SeekBar
 
        android:id="@+id/seekbar"
 
        android:layout_width="315dp"
 
        android:layout_height="wrap_content"
 
        android:max="100"
 
        android:layout_marginTop="20dp"
 
        android:progressDrawable="@drawable/seekbar_style"
 
        android:thumb="@drawable/my_seekbar_thumb"
 
        android:thumbOffset="0dp" />
 
 
</LinearLayout>
 
# 3 代码:
        public class MainActivity extends Activity {
 
 private TextView tv_hint;
 private SeekBar seekbar;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.fragment_main);
  tv_hint = (TextView) findViewById(R.id.tv_hint);
  tv_hint.setText("0");
 
  seekbar = (SeekBar) findViewById(R.id.seekbar);
  seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   
   @Override
   public void onStopTrackingTouch(SeekBar seekBar) {
   
   }
   
   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {
   
   }
   
   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser) {
    tv_hint.setText(progress+"");
    int width = tv_hint.getWidth();
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
    float i = (float) (2.65*progress+26);
    int left = dip2px(MainActivity.this, i)-width/2;
    lp.setMargins(left, 0, 0, 0);
    tv_hint.setLayoutParams(lp);
   
   }
  });
 
 }
 
 public int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }
}
    
 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你可以在布局文件中添加两个 Button 控件,如下所示: ``` <?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"> <EditText android:id="@+id/ipEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入服务器IP地址" /> <EditText android:id="@+id/portEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入服务器端口号" /> <Button android:id="@+id/connectButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="连接" /> <SeekBar android:id="@+id/seekBar_x" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:max="30" android:progress="0" /> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <SeekBar android:id="@+id/seekBar_y" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:max="30" android:progress="0" /> </ScrollView> <Button android:id="@+id/sendMessageButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送消息" /> <Button android:id="@+id/cancelButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消" /> <Button android:id="@+id/confirmButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确认" /> </LinearLayout> ``` 这样就在布局文件中添加了两个按钮:取消和确认。你可以根据需要修改按钮的文本和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值