约束布局在设备/模拟器上不可见

BRDroid

嗨,我的片段中有一个约束布局,我已经向它添加了视图绑定。

当我在设备上运行它时,UI 是空白的,但显示正确的片段标题

fragment_enter_details.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/purple_200"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".EnterDetailsFragment">

    <EditText
        android:id="@+id/et_first_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <EditText
        android:id="@+id/et_mobile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:layout_constraintTop_toBottomOf="@+id/et_first_name"/>

    <Button
        android:id="@+id/btn_verify_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Verify details"
        android:layout_margin="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

输入详细信息片段

class EnterDetailsFragment : Fragment() {

    private var _binding: FragmentEnterDetailsBinding? = null
    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

         _binding = FragmentEnterDetailsBinding.inflate(inflater, container, false)
        val view = binding.root
        initView()

        //val rootView = inflater.inflate(R.layout.fragment_enter_details, container, false)

        return view
    }

    private fun initView() {
        binding.btnVerifyDetails.setOnClickListener {
            findNavController().navigate(R.id.action_enterDetailsFragment_to_verifyDetailsFragment)
        }
    }
}

在gradle中添加了视图绑定

buildFeatures {
    viewBinding = true
}

导航图

这是我的导航图供参考

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/enterDetailsFragment">

    <fragment
        android:id="@+id/enterDetailsFragment"
        android:name="com.example.navjetpack.EnterDetailsFragment"
        android:label="fragment_enter_details"
        tools:layout="@layout/fragment_enter_details" >
        <action
            android:id="@+id/action_enterDetailsFragment_to_verifyDetailsFragment"
            app:destination="@id/verifyDetailsFragment" />
    </fragment>
    <fragment
        android:id="@+id/verifyDetailsFragment"
        android:name="com.example.navjetpack.VerifyDetailsFragment"
        android:label="fragment_verify_details"
        tools:layout="@layout/fragment_verify_details" />
</navigation>

关于我在这里可能做错的事情的蚂蚁想法

谢谢饶

编辑

主要活动

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navController = Navigation.findNavController(this, R.id.fragment)
        NavigationUI.setupActionBarWithNavController(this, navController)
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

<fragment
    android:id="@+id/fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:navGraph="@navigation/nav_graph"
    />

</androidx.constraintlayout.widget.ConstraintLayout>
极光

再次尝试约束你。我看到你限制了父级的底部到顶部。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

<fragment
    android:id="@+id/fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="parent" // Fix to app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:navGraph="@navigation/nav_graph"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

编辑文字提示在android studio设计预览中不可见,但在应用程序运行时在模拟器上可见

动画约束导致子视图布局在屏幕上可见

iOS 8模拟器在XCode 7 Beta中不可见

前景在真实设备中不可见,但在模拟器中可见

主菜单在移动设备上不可见,但在平板电脑上可见

约束布局在布局文件上不可见...并且在扩大其上的任何视图时出现错误

Android Studio 2.2约束布局按钮不可见

检测在模拟器或真实设备上运行的应用

视图在约束布局中不可见

ImageButton在模拟器或物理设备上的Android Studio上不可见

Qt:使布局的子代不可见/不可见

无效的视图控制器的布局不可见

设备模拟器-5554断开连接(Android Studio上没有模拟器)

在缺口设备模拟器上测试应用

AOSP x86_64模拟器在Android Studio中不可见

Sencha Touch字段集标签在模拟器上不可见

addSubview在设备上运行,在模拟器上崩溃

CoreData:错误:(14)在设备上,不在模拟器中

如何使按钮在模拟器上可见?

Debian 9 iptables设备在网络上不可见

Youtube在设备上失败,可在模拟器上运行

在实际设备上将其他布局设置为可见后,ImageButton不可见

在真实设备上运行模拟器与模拟器

AppCode无法在模拟器和设备上运行

代号一:模拟器上的用户界面布局和真正的Android设备完全不同

在较新的设备上,微调器选择菜单文本在白色背景上不可见/白色

Volley 在模拟器上运行但在设备上失败

为什么 RecyclerView 不会显示在设备上,但在布局检查器中可见?

ImageButton 在某些设备 Android Studio 上不可见