我想將音量添加到操作欄。 當用戶點擊該行動應該將行動起來吧內滑塊向右選擇體積 如何 ,我會做到這一點如何將音量滑塊添加到android動作欄?
1
A
回答
0
這裏有一個類似的問題的answer。他想添加完整的音頻控制按鈕。您會發現如何將控制Seekbar
添加到您的actionBar
以及如何處理它。
我希望它能幫助
5
來處理,這是使用ActionBar.setCustomView
最簡單的方法。
下面是控制媒體音量的例子:
首先,你需要創建一個包含SeekBar
的自定義佈局。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SeekBar
android:id="@android:id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</FrameLayout>
現在實現SeekBar.OnSeekBarChangeListener
在Activity
或Fragment
並宣佈了幾個變量:
/** Used to actually adjust the volume */
private AudioManager mAudioManager;
/** Used to control the volume for a given stream type */
private SeekBar mVolumeControls;
/** True is the volume controls are showing, false otherwise */
private boolean mShowingControls;
在onCreate
或任何你選擇,膨脹和自定義View
適用於ActionBar
。
// Control the media volume
setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Initialize the AudioManager
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Inflate the custom ActionBar View
final View view = getLayoutInflater().inflate(R.layout.view_action_bar_slider, null);
mVolumeControls = (SeekBar) view.findViewById(android.R.id.progress);
// Set the max range of the SeekBar to the max volume stream type
mVolumeControls.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
// Bind the OnSeekBarChangeListener
mVolumeControls.setOnSeekBarChangeListener(this);
// Apply the custom View to the ActionBar
getActionBar().setCustomView(view, new ActionBar.LayoutParams(MATCH_PARENT, MATCH_PARENT));
要切換的控件當按下你的MenuItem
,叫ActionBar.setDisplayShowCustomEnabled
不要忘記更新SeekBar
進步,你控制當前的音量。
// Toggle the custom View's visibility
mShowingControls = !mShowingControls;
getActionBar().setDisplayShowCustomEnabled(mShowingControls);
// Set the progress to the current volume level of the stream
mVolumeControls.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
要控制音量流,在OnSeekBarChangeListener.onProgressChanged
呼叫AudioManager.setStreamVolume
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Adjust the volume for the given stream type
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
最後在OnSeekBarChangeListener.onStopTrackingTouch
,刪除自定義控制正常再次顯示ActionBar
。
// Remove the SeekBar from the ActionBar
mShowingControls = false;
getActionBar().setDisplayShowCustomEnabled(false);
下面是您在按MenuItem
之前和之後的圖片。
相關問題
- 1. 將聲音添加到滑塊
- 2. 如何將滑塊添加到QMenu中?
- 3. 將DragScroll添加到滑塊
- 4. 音量滑塊
- 5. 如何將子菜單添加到Android動作欄sherlock
- 6. 如何將音軌音量滑塊綁定到物理音量按鈕
- 7. 如何將自動滑塊功能添加到我的jQuery滑塊?
- 8. 如何將自動播放添加到此滑塊
- 9. 如何將微調添加到動作欄的標題欄?
- 10. 添加一個動作欄到Theme.Black.NoTitleBar Android
- 11. android將背景添加到操作欄
- 12. 音量滑塊CustomControl
- 13. 如何將操作欄添加到DialogFragment?
- 14. 如何添加2音量滑塊到我的MP3播放器as3
- 15. 如何通過手機音量和進度滑塊調節音量滑塊?
- 16. 將動作添加到主工具欄
- 17. 如何將jquery ui滑塊添加到PrestaShop模塊表單
- 18. 如何將自定義CSS添加到滑動滑塊幻燈片
- 19. 創建滑塊更改android音量?
- 20. 如何添加填充到滑塊?
- 21. 如何追加到上滑塊滑動操作
- 22. 添加滑塊刪除工具欄
- 23. 將文本添加到jQuery滑塊
- 24. 將滑塊添加到電影中
- 25. 將滑塊添加到matplotlib圖表
- 26. 將鏈接添加到滑塊橫幅
- 27. WPF:將樣式添加到滑塊
- 28. 如何添加一個開關到android動作欄?
- 29. Android如何將圖標添加到操作欄?
- 30. Android:如何將兩個按鈕添加到操作欄中