我希望能够在不同的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