操作步骤:

  1. 在布局文件中添加一个按钮和一个线性布局(用于放置三个按钮):
<Button
    android:id="@+id/expand_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Expand" />

<LinearLayout
    android:id="@+id/expansion_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 3" />

</LinearLayout>
  1. 在Activity中找到按钮和线性布局:
val expandButton = findViewById<Button>(R.id.expand_button)
val expansionLayout = findViewById<LinearLayout>(R.id.expansion_layout)
  1. 给按钮添加点击事件,当点击按钮时展开/收起线性布局:
expandButton.setOnClickListener {
    if (expansionLayout.visibility == View.GONE) {
        expansionLayout.visibility = View.VISIBLE
    } else {
        expansionLayout.visibility = View.GONE
    }
}

完整代码:

布局文件:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/expand_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Expand"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"/>

    <LinearLayout
        android:id="@+id/expansion_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        android:layout_below="@+id/expand_button"
        android:layout_marginTop="16dp">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 3" />

    </LinearLayout>

</RelativeLayout>

Activity代码:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val expandButton = findViewById<Button>(R.id.expand_button)
        val expansionLayout = findViewById<LinearLayout>(R.id.expansion_layout)

        expandButton.setOnClickListener {
            if (expansionLayout.visibility == View.GONE) {
                expansionLayout.visibility = View.VISIBLE
            } else {
                expansionLayout.visibility = View.GONE
            }
        }
    }
}
``
设置一个按钮按下按钮后从按钮处向上延伸出来一个有三个按钮的界面同时原有界面不被遮挡使用kotlin语言在最新版android studio中编写请给出具体操作步骤和详细代码

原文地址: https://www.cveoy.top/t/topic/cHZf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录