Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中创建可在多个活动中访问的按钮?_Android_Android Layout_Audio Player_Android Music Player - Fatal编程技术网

如何在android中创建可在多个活动中访问的按钮?

如何在android中创建可在多个活动中访问的按钮?,android,android-layout,audio-player,android-music-player,Android,Android Layout,Audio Player,Android Music Player,我正在创建一个音乐播放器应用程序,以下是活动列表: 欢迎活动 歌曲稳定性 音乐活动 SongsListActivity将显示可用歌曲列表,单击该项目将带您进入MusicLayaActivity,这将触发SongService,SongService将在后台播放歌曲 下面是音乐表演 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="ma

我正在创建一个音乐播放器应用程序,以下是活动列表:

  • 欢迎活动
  • 歌曲稳定性
  • 音乐活动
  • SongsListActivity将显示可用歌曲列表,单击该项目将带您进入MusicLayaActivity,这将触发SongService,SongService将在后台播放歌曲
下面是音乐表演

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"  android:layout_height="match_parent"
    android:background="@drawable/background">

    <include layout="@layout/musicpanel"></include>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"

android:orientation="horizontal">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play"
    android:id="@+id/playPause"
    android:layout_gravity="bottom"
    android:layout_marginLeft="51dp"
    android:layout_marginStart="51dp"
    android:layout_alignTop="@+id/stop"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stop"
    android:id="@+id/stop"
    android:layout_gravity="bottom"
    android:layout_marginBottom="28dp"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/playPause"
    android:layout_toEndOf="@+id/playPause"
    android:layout_marginLeft="67dp"
    android:layout_marginStart="67dp" />
</RelativeLayout>

下面的代码用于musicpanel,它包含在MusicLayaActivity中

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"  android:layout_height="match_parent"
    android:background="@drawable/background">

    <include layout="@layout/musicpanel"></include>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"

android:orientation="horizontal">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play"
    android:id="@+id/playPause"
    android:layout_gravity="bottom"
    android:layout_marginLeft="51dp"
    android:layout_marginStart="51dp"
    android:layout_alignTop="@+id/stop"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stop"
    android:id="@+id/stop"
    android:layout_gravity="bottom"
    android:layout_marginBottom="28dp"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/playPause"
    android:layout_toEndOf="@+id/playPause"
    android:layout_marginLeft="67dp"
    android:layout_marginStart="67dp" />
</RelativeLayout>

如果用户在MusicLayaActivity中单击后退按钮,我们需要显示SongListActivity,所以这里我想显示musicpanel

我的问题是创建musicpanel的最佳实践是什么

现在我将它包含在xml中,但在每次活动中,我都需要为musicpanel的所有组件(如play、pause、next、previous)设置侦听器


提前感谢

您有几种选择:

  • 创建一个可重用的小部件。使用您的播放器布局、设置侦听器等,然后将其添加到您需要的任何位置
  • 将播放机移动到通知位置。它可以从应用程序中的任何位置访问。通知非常简单,必须在通知区域中,但您可以将此解决方案与1混合使用
  • 移动到片段或视图。您的播放器可以使用FrameLayout或RelativeLayout放置在主布局上方。这是我首选的解决方案
  • 将玩家移动到浮动窗口。这是浮动Facebook消息的头部是如何完成的。做起来很难,但是做得好的时候很好

  • 通常以1/2或2/3的方式完成,因为这些组合减少了代码重复,并且非常容易完成。

    我认为这对您来说是最好的。使用PageViwer

    1:制作2个碎片。 (1) 第一个活动 (2) 第二项活动

    2:在xml布局中使用PageViewer

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.8"
        android:orientation="vertical">
    
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>
    
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".2"
        android:background="@android:color/black"
        android:weightSum="3">
    
        <ImageView
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@android:drawable/ic_media_play" />
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@android:drawable/ic_media_pause" />
    
        <ImageView
            android:id="@+id/windowbtn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@android:drawable/ic_menu_revert" />
    
    
    </LinearLayout>
    
    这是多页适配器

       public class MultiPagerAdapter extends FragmentPagerAdapter {
    
    public MultiPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    
    @Override
    public Fragment getItem(int index) {
    
        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new FirstFragment();
        case 1:
            // Games fragment activity
            return new SecFragment();
        }
    
        return null;
    }
    
    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 2;
    }
    

    }

    你想说musicpanel在SonglistActivity和MusicPlayActivity中都应该可用吗?