嗨,我在我的片段中有一個約束性的布局,我已經為它添加了視圖系結。
當我在設備上運行它時,用戶界面是空白的,但顯示正確的片段標題
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"/span>
android:layout_height="match_parent"/span>
android:background="@color/purple_200"/span>
xmlns:app="http://schemas.android.com/apk/res-auto"/span>
tools:context=".EnterDetailsFragment">
<編輯文本
android:id="@ id/et_first_name"。
android:layout_width="match_parent"。
android:layout_height="wrap_content"/span>
android:layout_margin="10dp"/span>
app:layout_constraintBottom_toBottomOf="parent"。
app:layout_constraintLeft_toLeftOf="parent"。
app:layout_constraintRight_toRightOf="parent"。
app:layout_constraintTop_toTopOf="father"/>
<編輯文本
android:id="@ id/et_mobile"/span>
android:layout_width="match_parent"。
android:layout_height="wrap_content"/span>
android:layout_margin="10dp"/span>
app:layout_constraintTop_toBottomOf="@ id/et_first_name" />
<按鈕
android:id="@ id/btn_verify_details"/<按鈕
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
android:text="驗證細節"。
android:layout_margin="10dp"/span>
app:layout_constraintBottom_toBottomOf="father"。
app:layout_constraintLeft_toLeftOf="parent"。
app:layout_constraintRight_toRightOf="parent"。
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
EnterDetailsFragment
class EnterDetailsFragment : Fragment( ) {
private var _binding: FragmentEnterDetailsBinding? = null
private val binding get() = _binding!
override fun onCreate(servedInstanceState: Bundle? ){
super.onCreate(s savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
膨脹器。LayoutInflater, container: ViewGroup?
savedInstanceState。Bundle?
)。視圖 {
_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中添加了viewbinding
buildFeatures {
viewBinding = true
導航圖
這是我的導航圖,供參考
。
<?xml version="1.0"/span> encoding="utf-8"/span>?>
<導航 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"/span>
android:id="@ id/nav_graph"。
app:startDestination="@id/enterDetailsFragment">
<片段
android:id="@ id/enterDetailsFragment"/span>
android:name="com.example.navjetpack.EnterDetailsFragment"。
android:label="fragment_enter_details"。
tools:layout="@layout/fragment_enter_details" >
<行動
android:id="@ id/action_enterDetailsFragment_to_verifyDetailsFragment"。
app:destination="@id/verifyDetailsFragment" />
</fragment>
<片段
android:id="@ id/verifyDetailsFragment"/span>
android:name="com.example.navjetpack.VerifyDetailsFragment"。
android:label="fragment_verify_details"。
tools:layout="@layout/fragment_verify_details" />
</navigation>
想知道我在這里可能做錯了什么
。
謝謝
Rao
編輯
MainActivity
class MainActivity : AppCompatActivity(){
override fun onCreate(sedInstanceState: Bundle? ){
super.onCreate(s 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"/span>
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
tools:context=".MainActivity">
<片段
android:id="@ id/fragment"。
android:name="androidx.navigation.fragment.NavHostFragment"/span>
android:layout_width="0dp"/span>
android:layout_height="0dp"/span>
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>
uj5u.com熱心網友回復:
再試一下約束你的。我看到你將底部約束到父體的頂部。
<?xml version="1.0"/span> encoding="utf-8"/span>?
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"?
xmlns:app="http://schemas.android.com/apk/res-auto"/span>
xmlns:tools="http://schemas.android.com/tools"/span>
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
tools:context=".MainActivity">
<片段
android:id="@ id/fragment"。
android:name="androidx.navigation.fragment.NavHostFragment"/span>
android:layout_width="0dp"/span>
android:layout_height="0dp"/span>
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/318405.html
標籤:
上一篇:用戶資訊不顯示在文本視圖中
