自定义 NavHostFragment 将框架布局替换为 viewgorup



布局中有一些视图和一个FrameLayout。我想自定义NavHostFragmentRelativeLayout替换FrameLayout,它可以工作,但视图层次结构错误。图像视图应该在顶部,但它在底部。

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/logo" />
</RelativeLayout>

和我的定制NavHostFragment

public abstract class BaseModuleFragment extends NavHostFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = null;
if (getContentViewId() > 0) {
view = inflater.inflate(getContentViewId(), container, false);
View frameLayout = view.findViewById(R.id.lb_nav_host_container);
if (!(frameLayout instanceof FrameLayout)) {
throw new RuntimeException("frame layout must exist");
}
frameLayout.setId(getId());
} else {
view = super.onCreateView(inflater, container, savedInstanceState);
}
return view;
}
}

是方法错了,还是怎么解决?

将布局更改为此布局。应该没问题

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_below="@+id/ivTop"
android:id="@id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/ivTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/logo" />
</RelativeLayout>

这些图片显示:

  • 错: 框架布局封面图像视图,这是错误的

  • 右 图像视图应该是顶部,这是正确的

  • FrameLayout top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/logo" />
<FrameLayout
android:id="@id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
  • ImageView top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/logo" />
</RelativeLayout>

最新更新