如何在 android kotlin 上关闭 include 标签中的 xml 文件

问题描述 投票:0回答:1

我想在点击 kotlin 中包含的 xml 文件的关闭按钮时关闭在 include 标签中打开的 xml 文件。我尝试如下。我无法关闭。有人可以帮我在 kotlin 中做到这一点吗?

我的activity_main.xml

<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">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">
<ImageView
            android:id="@+id/openLayout"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/baseline_keyboard_arrow_up_24"/>
    </LinearLayout>
<include
    android:id="@+id/inc"
    layout="@layout/include_layout_view"/>
</androidx.constraintlayout.widget.ConstraintLayout>

我的include_layout_view.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/orange_light"
    android:gravity="center"
    android:id="@+id/wraper"
    android:visibility="gone">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/close"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/baseline_close_24"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Button"/>

</LinearLayout>

和我的 MainActivity.kt 文件

MainActivity 类:AppCompatActivity() { 私有 Lateinit var 绑定:ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.openLayout.setOnClickListener {

            binding.inc.root.visibility = View.VISIBLE

        }
        val view = LayoutInflater.from(this).inflate(R.layout.include_layout_view,null)
        val close = view.findViewById<Button>(R.id.btn1)

        close.setOnClickListener {
            Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show()
            it.visibility = View.GONE
        }
    }
}
android kotlin android-studio android-layout android-xml
1个回答
0
投票

val view = LayoutInflater.from(this).inflate(R.layout.include_layout_view,null)

您正在创建一个新实例

尝试\

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.openLayout.setOnClickListener {
            binding.inc.visibility = View.VISIBLE
        }

        val closeButton = binding.inc.findViewById<ImageView>(R.id.close) // Access close button from included layout
        closeButton.setOnClickListener {
            Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show()
            binding.inc.visibility = View.GONE
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.