如何在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/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中都应该可用吗?