添加片段以显示全屏布局

时间:2018-04-16 11:19:34

标签: android

我想在用户点击歌曲时使用片段显示正在播放的屏幕。我有一个main_activity布局显示歌曲列表和一个项目点击监听器。现在我想打开另一个完全占用的布局屏幕显示正在播放的当前歌曲。我想我可以使用片段来实现这一点,但无法找到实现它的方法。

这是我的main_activity xml文件

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"/>``
</RelativeLayout>

现在我想用现在播放的布局替换整个布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme.NoActionBar">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="320dp"
    android:fitsSystemWindows="true"
    android:id="@+id/image"/>

1 个答案:

答案 0 :(得分:1)

你的main_activity.xml应该是这样的:

  <FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_frame"

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
  >

<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true"
 tools:context=".MainActivity"
 tools:showIn="@layout/activity_main">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>``
</RelativeLayout>

</FrameLayout>

点击按钮,您已执行此代码:

  Fragment fragment = new NowPlayingFragment();


 ((FragmentActivity) view.getContext()).getFragmentManager().beginTransaction()
                .replace(R.id.main_frame, fragment).addToBackStack(null).commit();

所以这将取代当前活动的片段

编辑: 这是我的片段类

public class NowPlayingFragment extends android.support.v4.app.Fragment {

public NowPlayingFragment() {
}
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_playing, container, false);
}