FrameLayout中TextView在虚拟设备不显示的问题求助
问题:Fragment间的TextView在虚拟设备中不显示(设计视图可见)
我在FrameLayout承载的Fragment序列中放置了一个"Forgot password?"的TextView,它在Android Studio设计视图中正常显示,但在虚拟设备运行应用时完全不可见。尝试过以下方法均无效:
- 将TextView放入FrameLayout容器
- 为TextView单独创建Fragment(应用启动直接崩溃)
- 用SwipeRefreshLayout包裹布局
布局代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="385dp" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".RegistrationContainerFragment" android:orientation="vertical" android:background="@drawable/auth_container_background" android:elevation="8dp" android:translationZ="4dp" android:layout_marginHorizontal="15dp"> <FrameLayout android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@+id/username_and_email_input_container" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@+id/password_input_container" android:layout_width="match_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@+id/primary_button_container" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:fontFamily="@font/montserrat_semibold_font" android:textSize="14sp" android:textColor="@color/text" android:text="Forgot password?"/> <FrameLayout android:id="@+id/line" android:layout_width="match_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@+id/textAndLinkText" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"/> </LinearLayout>
逻辑代码
import android.os.Bundle import android.text.InputType import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentTransaction class LoginContainerFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_registration_container, container, false) val fragmentManager: FragmentManager = childFragmentManager val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction() val title = TitleFragment() title.setShowBackArrow(true) title.setTitleText("Log In") fragmentTransaction.add(R.id.title, title) val textInputFragment = InputFragment() textInputFragment.setInputType(InputType.TYPE_CLASS_TEXT) textInputFragment.setInputHint("Enter your email or username") fragmentTransaction.add(R.id.text_input_container, textInputFragment) val passwordInputFragment = InputFragment() passwordInputFragment.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD) passwordInputFragment.setInputHint("Enter your password") fragmentTransaction.add(R.id.password_input_container, passwordInputFragment) val primaryButtonFragment = PrimaryButtonFragment() primaryButtonFragment.setButtonText("next") fragmentTransaction.add(R.id.primary_button_container, primaryButtonFragment) val lineFragment = LineFragment() fragmentTransaction.add(R.id.line, lineFragment) val textAndLinkFragment = TextWithLinkFragment() textAndLinkFragment.setTextAndLinkText( getString(R.string.dont_have_an_account), getString(R.string.sign_in) ) fragmentTransaction.add(R.id.textAndLinkText, textAndLinkFragment) fragmentTransaction.commit() return view } }
问题分析
- 布局高度限制:LinearLayout设置了固定高度
385dp,运行时Fragment加载后会占用实际高度,当总内容高度超过385dp时,TextView会被挤出可视区域;而设计视图未加载Fragment内容,所以能显示TextView。 - Fragment容器ID不匹配:逻辑代码中尝试向
R.id.text_input_container添加Fragment,但布局中对应的ID是username_and_email_input_container,这会导致该Fragment加载失败,虽不直接导致TextView消失,但会影响布局结构。
解决方案
1. 修复布局高度限制
将LinearLayout的固定高度改为自适应内容的wrap_content,避免TextView被挤出:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <!-- 其他属性不变 --> android:layout_height="wrap_content"> <!-- 修改此处 -->
2. 修正Fragment容器ID错误
把逻辑代码中的R.id.text_input_container改为布局中实际存在的R.id.username_and_email_input_container:
// 原代码 // fragmentTransaction.add(R.id.text_input_container, textInputFragment) // 修改为 fragmentTransaction.add(R.id.username_and_email_input_container, textInputFragment)
3. 优化TextView布局(可选)
给TextView添加间距属性,避免和上下Fragment挤在一起,提升显示效果:
<TextView <!-- 其他属性不变 --> android:layout_marginVertical="16dp" android:paddingHorizontal="16dp"/>
4. 解决TextView Fragment崩溃问题(若需用Fragment承载)
如果仍想通过Fragment显示TextView,确保Fragment实现正确:
步骤1:创建TextView的Fragment类
class ForgotPasswordFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_forgot_password, container, false) } }
步骤2:创建对应的布局文件fragment_forgot_password.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="@font/montserrat_semibold_font" android:textSize="14sp" android:textColor="@color/text" android:text="Forgot password?" android:layout_marginVertical="16dp" android:paddingHorizontal="16dp"/>
步骤3:在布局中添加对应容器
<FrameLayout android:id="@+id/forgot_password_container" android:layout_width="match_parent" android:layout_height="wrap_content"/>
步骤4:在容器Fragment中添加该Fragment
val forgotPasswordFragment = ForgotPasswordFragment() fragmentTransaction.add(R.id.forgot_password_container, forgotPasswordFragment)
内容的提问来源于stack exchange,提问作者Milena Tsymbal
相关产品推荐
相关产品推荐

