android - 约束布局在设备/模拟器上不可见
问题描述
嗨,我的片段中有一个约束布局,我已经向它添加了视图绑定。
当我在设备上运行它时,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>
推荐阅读
- java - 无法选择要在 ImageView 中显示的图像
- websocket - 如何连接这个 websocket 服务器
- c# - 如何在每个房间放置单独的摄像头以显示玩家的位置?
- python - 使用 asyncio/pyppeteer 时打开文件过多错误
- function - 为什么我的脚本中出现函数错误?
- entity-framework - 使用依赖注入时使用存储库模式的多个 DB 上下文与 UnitofWork
- reactjs - SVG 笔划未显示在 clipPath 内部
- mongodb - 配置 AuthenticationManagerBuilder 以使用用户存储库
- facebook - 在使用 Facebook 的 Oauth 和 Python Flask 作为后端服务时卡住了
- azure - 使用 Graph API 创建 Azure SSO 应用程序