android - 以编程方式搜索栏

标签 android seekbar

我正在像这样以编程方式创建一个SeekBar:

SeekBar seekBar = new SeekBar(this);
seekBar.setMax(15);
seekBar.setIndeterminate(true);
seekBar.setMinimumWidth(200);

ShapeDrawable thumb = new ShapeDrawable(new OvalShape());

thumb.getPaint().setColor(0x00FF00);
thumb.setIntrinsicHeight(80);
thumb.setIntrinsicWidth(30);

seekBar.setThumb(thumb);
seekBar.setProgress(1);

但是我只能看到一个小的 SeekBar
可能是什么问题?

谢谢

最佳答案

试试下面的代码

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Sample2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        SeekBar seekBar = new SeekBar(this);
        seekBar.setMax(15);
//      seekBar.setIndeterminate(true);

        ShapeDrawable thumb = new ShapeDrawable(new OvalShape());

        thumb.setIntrinsicHeight(80);
        thumb.setIntrinsicWidth(30);
        seekBar.setThumb(thumb);
        seekBar.setProgress(1);
        seekBar.setVisibility(View.VISIBLE);
        seekBar.setBackgroundColor(Color.BLUE);

        LayoutParams lp = new LayoutParams(200, 50);
        seekBar.setLayoutParams(lp);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                System.out.println(".....111.......");

            }

            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                System.out.println(".....222.......");
            }

            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                // TODO Auto-generated method stub
                System.out.println(".....333......."+arg1);
            }
        });
        setContentView(seekBar);
    }
}

谢谢 迪帕克

关于android - 以编程方式搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6343116/

相关文章:

java - Android Studio : Virtual Device

android - 在Android上使用Webview浏览YouTube channel 是否可以?

android seekbar progressdrawable : background bitmap doesnt stretch (height)

java - 为什么我的表情给出了错误的答案?

Android 搜索栏 : Changing the value

android - 如何在 Android 中通过 Data API v3.0 使用 videoID 从 youtube 检索单个视频的详细信息?

Android:使用互联网发送短信

android - GridView.setOnItemClickListener 在 GridView 中不起作用

android - 我们可以在不影响设备音量的情况下将Android应用的音量设置为相对于设备的音量吗?

android - 为什么我的 Dialog Seekbar 崩溃了?空指针异常