下拉菜单展开

时间:2019-02-27 08:01:49

标签: android android-layout

我想填写这张表格。

事物


事物

如果单击该东西,

事物

  • 事物1

  • 事物2


事物

像这样

但是我只能找到向左或向右展开的菜单。 如何实现下拉菜单?

1 个答案:

答案 0 :(得分:1)

这是实现目标的众多方式之一

第1步:将以下依赖项放入您的应用gradle文件中。

    implementation 'com.github.aakira:expandable-layout:1.4.2@aar'

第2步:您的布局可以如下设计:

     <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/card_id"
            android:layout_marginTop="5dp"
            app:cardElevation="4dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="horizontal"                  
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"                  
                    android:text="Thing"
                    android:textAlignment="viewStart"/>

            </LinearLayout>

        </android.support.v7.widget.CardView>

    <com.github.aakira.expandablelayout.ExpandableRelativeLayout
        android:id="@+id/expandableLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:ael_duration="400"          
        app:ael_expanded="false"
        app:ael_interpolator="fastOutSlowIn"
        app:ael_orientation="vertical"
        android:elevation="4dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_marginLeft="15dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="none"            
                android:scrollHorizontally="false"
                android:text="Thing 1" />

                <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="none"            
                android:scrollHorizontally="false"
                android:text="Thing 2" />

        </LinearLayout>

    </com.github.aakira.expandablelayout.ExpandableRelativeLayout>

第3步:

声明视图。

    CardView cardView;
    ExpandableRelativeLayout expandableLayout;

绑定视图并在onCreate()方法中的CardView上注册onClickListener

     cardView = findViewById(R.id.card_id);
     expandableLayout = findViewById(R.id.expandableLayout1);

     cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            expandableLayout.toggle(); // toggle expand and collapse
        }
    });