Android 分裂活动
我第一次使用片段,我试图从一个活动导航到另一个片段。导航应该由OnClickListener(ImageView)处理,它将我带到所述片段。然而,我的片段并没有占据整个屏幕。我已经在传入视图上设置了match parent,以及应该保存片段的帧。所以总的来说,我不确定我是否对XML或Java有误解。我会在下面尽我所能提供Android 分裂活动,android,android-fragments,Android,Android Fragments,我第一次使用片段,我试图从一个活动导航到另一个片段。导航应该由OnClickListener(ImageView)处理,它将我带到所述片段。然而,我的片段并没有占据整个屏幕。我已经在传入视图上设置了match parent,以及应该保存片段的帧。所以总的来说,我不确定我是否对XML或Java有误解。我会在下面尽我所能提供 public class recipe_detail extends AppCompatActivity { TextView mIngredientsTV;
public class recipe_detail extends AppCompatActivity {
TextView mIngredientsTV;
RecyclerView ingredientsRecyclerView;
RecyclerView stepsRecyclerView;
ImageView mBakingGif;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
mBakingGif.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.frag_container, new VideoFragment());
ft.commit();
}
});
}
public class VideoFragment extends Fragment {
ImageView mThumbnailImage;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.video_fragment, container, true);
}
// This event is triggered soon after onCreateView().
// Any view setup should occur here. E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
mThumbnailImage = view.findViewById(R.id.exo_player_view);
Drawable myDrawable = getResources().getDrawable(R.drawable.ic_launcher_background);
mThumbnailImage.setImageDrawable(myDrawable);
}
}
您正在膨胀的碎片将始终占据与容纳它的容器相同的空间。在您的情况下,容器(FrameLayout)的高度与包裹内容相同。因此,片段将只占用显示其内容所需的空间量。您可以做的是将FrameLayout的高度设置为MATCH_PARENT。这样,片段将占据整个屏幕空间
请注意,在另一个视图上添加片段时,请始终记住为片段布局提供一些背景色(默认情况下为透明)。否则,您以前的视图也将通过片段保持可见 编辑(解释评论) 您需要像这样将FrameLayout移出ScrollView
<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">
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/ingredients_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/ingredients_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Ingredients_header"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold"
tools:text="INGREDIENTS" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:background="#E0F2F1"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/ingredients_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/steps_label"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/video_thumbnail"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="20dp"
android:background="@color/colorPrimaryDark" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
<!-- Frame to hold fragment-->
<FrameLayout
android:id="@+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
我所做的是将片段容器移出ScrollView。但是,由于一个活动/片段只能有一个根元素,因此我添加了一个FrameLayout作为ScrollView和FrameLayout的父元素。“请注意,在另一个视图上添加片段时,请始终记住为片段布局提供一些背景色(默认情况下是透明的)。否则,您以前的视图也将通过片段保持可见。“”注意…..但是,我已将换行内容更改为匹配父对象,现在我有一个IllegalStateException:指定的子对象已具有父对象。您必须调用removeView()首先,在孩子的父项上,我已修复了IllegalStateException,并将换行内容更改为与父项匹配,但片段仍在整个屏幕的一半处出现。@pureskill75将以下属性赋予FrameLayout。android:layout\u alignParentTop=“true“我不打岔。但是,如果我在传入的布局中更改视图的维度,会有移动,但我不知道这是否是处理UI的好方法?我认为问题是因为片段容器位于滚动视图中。作为一个小测试,将片段容器移动到滚动视图之外,并将其宽度和高度设置为与父对象匹配。
<!--Fragment to be infalted-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/exo_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/exo_player_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/description_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/exo_player_view" />
<TextView
android:id="@+id/short_description_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/description_tv" />
<Button
android:id="@+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Prev" />
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Next" />
</RelativeLayout>
<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">
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/ingredients_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/ingredients_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Ingredients_header"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold"
tools:text="INGREDIENTS" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:background="#E0F2F1"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/ingredients_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/steps_label"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/video_thumbnail"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="20dp"
android:background="@color/colorPrimaryDark" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
<!-- Frame to hold fragment-->
<FrameLayout
android:id="@+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>