如何添加音量滑块到Android的动作吧? [英] How to add volume slider to android action bar?

查看:180
本文介绍了如何添加音量滑块到Android的动作吧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要补充血容量行动吧。 当用户点击这一行动应该把滑块内操作栏中选择权 卷 如何,我会做到这一点。

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 Fragmentand 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.

这篇关于如何添加音量滑块到Android的动作吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆