我的应用具有音量偏好,低于我的布局代码:
<VolumePreference
android:name="Volume Preference"
android:summary="Ringtone Volume"
android:title="Ringtone Volume"
android:key="ringVolPref"/>
但我不知道,如何在课堂上实现这一点?
答案 0 :(得分:1)
试试此代码
<强> XML 强>
<SeekBar android:id="@+id/seekbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:minWidth="250px" />
<强> CODE 强>
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.seekbar);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
@Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
答案 1 :(得分:0)
在您的情况下,最好的方法是添加到.xml android:streamType
。这将更改所选流类型的音量。
<VolumePreference
android:title="Alarm volume"
android:persistent="false"
android:streamType="alarm"/>