我希望能夠在不同的Android活動之間共享視圖。這個觀點是一個音樂播放器,我想永遠站在每一個活動的尾部。我也希望能夠從任何類訪問它,所以我從我的MainActivity靜態引用它。該視圖被稱爲Player
。在Android活動之間共享視圖的正確方法?
我的MainActivity將其設置....
public class MainActivity extends Activity{
public static Player player;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
MainActivity.player = new Player(this);
}
Player類是由充氣我player.xml
文件進行。
public class Player extends LinearLayout{
private ImageView previousButton, playButton, nextButton, playlistButton;
private TextView songTitle;
public Player(Context context)
{
super(context);
init(context);
}
private void init(Context context)
{
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.player, this);
this.previousButton = (ImageView) view.findViewById(R.id.playerPreviousButton);
this.playButton = (ImageView) view.findViewById(R.id.playerPlayButton);
this.nextButton = (ImageView) view.findViewById(R.id.playerNextButton);
this.playlistButton = (ImageView) view.findViewById(R.id.playerPlaylistButton);
this.songTitle = (TextView) view.findViewById(R.id.playerSongTitle);
}
如何跨多個活動分享此內容?我在我的Player類中有很多函數,我沒有列出我需要能夠從任何類訪問,而不僅僅是活動類,因此我儘管去了靜態路由,並且只進行了一次初始化。
有人可以幫助我讓我知道正確的方式去做這件事嗎?
您需要爲此實現服務。在服務類中編寫你的函數。從主要活動中啓動服務,然後從應用程序中的任何活動調用函數。 – AndyN
爲了分享您的觀點,請爲您的播放器創建一個佈局文件,並將該佈局文件包含在每個活動的佈局文件中。 – AndyN