我要补充血容量行动吧。 当用户点击这一行动应该把滑块内操作栏中选择权 卷 如何,我会做到这一点。
I want to add volume to action bar. When user clicks on that action it should place slider inside action bar for selecting right volume How would i do that
来处理最简单的方法是使用 ActionBar.setCustomView
。
The easiest way to handle this is to use ActionBar.setCustomView
.
下面是用于控制媒体音量的例子:
Here's an example that controls the media volume:
首先,你需要创建一个包含搜索栏
在自定义布局。
First, you'll need to create the custom layout that contains your 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
在活动
或片段
并宣布了几个变量:
Now implement SeekBar.OnSeekBarChangeListener
in your Activity
or Fragment
and declare a couple of variables:
/** 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
或你选择,膨胀和自定义查看
适用于动作条
。
In onCreate
or wherever you choose to, inflate and apply the custom View
to the 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));
要切换的控件,当你的菜单项
是pressed,调用 ActionBar.setDisplayShowCustomEnabled
和不忘记更新搜索栏
进度,你是在控制当前的音量。
To toggle the controls when your MenuItem
is pressed, call ActionBar.setDisplayShowCustomEnabled
and don't forget to update the SeekBar
progress to the current volume level you're controlling.
// 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
To control the volume stream, in OnSeekBarChangeListener.onProgressChanged
call 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
,删除自定义的控件来显示动作条
恢复正常。
And finally in OnSeekBarChangeListener.onStopTrackingTouch
, remove the custom controls to display the ActionBar
normally again.
// Remove the SeekBar from the ActionBar
mShowingControls = false;
getActionBar().setDisplayShowCustomEnabled(false);
下面是之前的图片,你preSS后的菜单项
。
Here's a picture of before and after you press the MenuItem
.
上一篇:重新选择在动作条微调项动作