2014-02-28 76 views

回答

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.OnSeekBarChangeListenerActivityFragment並宣佈了幾個變量:

/** 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之前和之後的圖片。

enter image description here