在我的MainActivity onCreateView中,我以编程方式创建了一个搜索栏(没有xml):
mVolControlSB = new SeekBar(context);
mVolControlSB.setMax(maxVolume);
mVolControlSB.setProgress(curVolume);
mVolControlSB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
Log.d("MainActivity", "volControlSB.setOnSeekBarChangeListener onStopTrackingTouch arg0="+arg0);
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
Log.d("MainActivity", "volControlSB.setOnSeekBarChangeListener onStartTrackingTouch arg0="+arg0);
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
mVolControlSB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0, 20));
kitctrlLayout.addView(mVolControlSB);
显示搜索栏,但拇指位置错误(maxVolume为15,curVolume在我的示例中为10,但它一直显示在左侧)。另外,当我拖动拇指时没有调用回调,没有任何反应。我使mVolControlSB成为MainActivity类中的静态字段,但我也尝试将其作为本地
static SeekBar mVolControlSB;
有没有人有想法?
请注意,我应该添加我使用标签布局,并在
中调用此代码 public static class DummySectionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
感谢
答案 0 :(得分:0)
这样对我来说很好。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.lay);
mVolControlSB = new SeekBar(this);
mVolControlSB.setMax(15);
mVolControlSB.setProgress(10);
mVolControlSB
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
Log.d("MainActivity",
"volControlSB.setOnSeekBarChangeListener onStopTrackingTouch arg0="
+ arg0.getProgress());
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
Log.d("MainActivity",
"volControlSB.setOnSeekBarChangeListener onStartTrackingTouch arg0="
+ arg0.getProgress());
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1,
boolean arg2) {
}
});
mVolControlSB.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
ll.addView(mVolControlSB);
}