【问题标题】:how to create buttons which can be acciable in multiple activites in android?如何创建可以在 android 的多个活动中访问的按钮?
【发布时间】:2016-01-04 20:16:17
【问题描述】:

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

  • WelcomeActivity
  • SongsListActivity
  • 音乐播放活动
  • SongsListActivity 将显示可用歌曲列表,点击项目后,您将转到 MusicPlayActivity,这将触发 SongService,进而在后台播放歌曲。

下面是MusicPlayActivity

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

以下代码适用于 MusicPlayActivity 中包含的音乐面板

<?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>

如果用户从 MusicPlayActivity 中点击返回按钮,我们需要显示 SongListActivity 所以这里我想显示音乐面板。

我的问题是创建音乐面板的最佳做法是什么?

现在我将它包含在 xml 中,但在每个活动中,我每次都需要为音乐面板的所有组件(如播放、暂停、下一个、上一个)设置侦听器

提前致谢

【问题讨论】:

  • 您想说音乐面板应该在 SongListActivty 和 MusicPlayActivty 中都可用?

标签: android android-layout audio-player android-music-player


【解决方案1】:

您有多种选择:

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

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

【讨论】:

    【解决方案2】:

    我认为它最适合你。使用 PageViwer

    1:制作 2 个片段。 (1)firstActivity (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>
    

    这是您的 Main Activity 中的 viewPager 实例。

                viewPager = (ViewPager) findViewById(R.id.pager);
        // viewPager.setPageTransformer(true, new VerticalPageTransformer());
    
        mAdapter = new MultiPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
            @Override
            public void onPageSelected(int position) {
    
            }
    
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }
    
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        viewPager.setOnTouchListener(new View.OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                viewPager.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
    
    }
    

    这里是多页适配器

       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;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 2017-01-18
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多