我手動創建了一個片段。但不能宣告像 web 視圖或按鈕這樣的組件。當我粘貼并編輯“val webview:WebView ...”時,也會出現錯誤“Fragment(R.layout.fragment_htmlviewer)”。我不知道該怎么做,當我從 android studio 創建時,它看起來很復雜。所以我看了一個視頻,這更容易,但不能宣告組件。我非常初學者所以請幫助我...我的代碼:
fragment_htmlviewer.xml
<WebView
android:id="@ id/htmlViewer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:contentDescription="@string/fabdescription"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_left_arrow_icon" />
HtmlViewerFragment.kt
class HtmlViewerFragment : Fragment(R.layout.fragment_htmlviewer) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val webview: WebView = getView()!!.findViewById<View>(R.id.htmlViewer) as WebView
}
}
uj5u.com熱心網友回復:
我推薦你使用系結結構,它是今天更受歡迎的結構。build.gradle(模塊)
buildFeatures {
viewBinding true
dataBinding true
}
HtmlViewerFragment
private var _binding: FragmentHtmlViewerBinding? = null
private val binding
get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentHtmlViewerBinding.inflate(inflater, container, false)
//.. this is write code
binding.apply{
//eg:
htmlViewer.setOnClickListener{
}
}
return binding.root
}
uj5u.com熱心網友回復:
override fun onStart() {
super.onStart()
htmlViewer = requireView().findViewById(R.id.htmlViewer) as WebView
htmlViewer!!.loadUrl("https://www.google.com")
}
這是解決方案。
htmlViewer = requireView().findViewById(R.id.htmlViewer) as WebView
固定的
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471138.html
