【问题标题】:Can we write UI in activity xml as well UI in fragment xm?我们可以在活动 xml 中编写 UI 以及在片段 xm 中编写 UI 吗?
【发布时间】:2021-09-08 21:22:19
【问题描述】:

我创建了一个活动并有 2 个按钮。单击每个按钮片段会打开。 但是当我这样做时,片段 UI 会与活动 UI 重叠。 我想知道我们是否可以在活动和片段中都有 UI 组件。 此外,如果必须将活动 UI 保持空白并从 UI 的活动创建一个新片段。 请让我知道如何解决这个问题。 代码示例:

MainActivity.kt

 override fun onClick(view: View?) {
        Log.d(TAG, "onClick: ")
        when(view!!.id){
            R.id.song_list->
            {
                Toast.makeText(this, "Songs list will be shown", Toast.LENGTH_SHORT).show()
                openFragment("Songs")
            }
            R.id.artist_list->
            {
                Toast.makeText(this, "Artist list will be shown", Toast.LENGTH_SHORT).show()
                openFragment("Artist")
            }
        }
    }

    fun openFragment(fragmentName: String){
        Log.d(TAG, "openFragment: ")
        if(fragmentName.equals("Songs")) {
            val fragmentManager = supportFragmentManager
            val transaction = fragmentManager.beginTransaction()
            transaction.replace(R.id.main_fragment, SongsFragment())
            transaction.disallowAddToBackStack()
            transaction.commit()
        }else{
            val fragmentManager = supportFragmentManager
            val transaction = fragmentManager.beginTransaction()
            transaction.replace(R.id.main_fragment, ArtistFragment())
            transaction.commit()
        }
    }

MainActivity 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">

    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/song_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Songs List"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <Button
        android:id="@+id/artist_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Artist List"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/song_list"
        android:layout_marginTop="5dp"/>

SongsFragment.kt

 override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(TAG, "onCreate: ")
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_songs, container, false)
        Log.d(TAG, "onCreateView: ")
    }

SongsFragment 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"
    tools:context=".SongsFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Please find the list of songs here" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: java android kotlin android-fragments android-activity


    【解决方案1】:

    正如我所见,您在保存片段的框架布局上缺少一些约束,如果您按照自己的喜好进行设置,您可以同时拥有您的按钮和您选择的片段。

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 2010-11-25
      • 2012-11-18
      相关资源
      最近更新 更多