【发布时间】:2020-11-15 16:56:48
【问题描述】:
我计划为配置文件片段提供两种布局版本,一种用于新用户,另一种用于登录用户。
如何在 Fragment 的 onCreateView() 方法中动态切换/选择必要的布局?
想到的最直接的想法是使用两个<include> 布局并根据变量将其中一个隐藏在onCreateView() 中。但我想知道是否有更聪明的方法。
当前布局 xml:
主要内容在第二个FrameLayout里面,我想有两个版本可供选择。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<ImageView
android:id="@+id/profile_monk_head_imageview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:contentDescription="@string/profile_monk_image_content_desc"
android:src="@drawable/monk_head" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- This is what I want to switch out -->
<FrameLayout ... >
</androidx.core.widget.NestedScrollView>
</FrameLayout>
【问题讨论】:
标签: android android-viewbinding