如果自定义一个可以点击之后展开/收缩的菜单组件
首先,定义布局文件,如下
xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_alignParentBottom="true" android:layout_height="68dp"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="68dp" android:id="@+id/bottompic" android:background="@drawable/bottombackground" > <ImageView android:id="@+id/iv_bottomimage" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="@drawable/aacon" android:layout_marginLeft="20dp" /> <TextView android:id="@+id/tv_bottomtext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/iv_bottomimage" android:text="@string/_sbottomtext" android:textColor="@color/white" /> <ImageView android:id="@+id/tv_bottomimage1" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@drawable/menuicon" android:layout_marginRight="20dp" /> RelativeLayout> <LinearLayout android:id="@+id/menulist" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/bottompic" > <ListView android:id="@+id/bottomlistview" style="@style/listmenutype" android:listSelector="@drawable/griditemselecter" >ListView> LinearLayout> RelativeLayout>
listview是用来显示展开之后的菜单项的
然后为组件设置点击事件,这很简单,就不贴代码了
重点是点击之后的展开和隐藏
主要是以下的代码
展开:
private void open() { RelativeLayout.LayoutParams mlayoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); mlayoutparams.addRule(RelativeLayout.BELOW, R.id.headlayout1); mrelativelayout.setLayoutParams(mlayoutparams); mswitch = !mswitch; }
其中需要注意的就是layoutparams.addrule方法了,将其设置为在顶部布局的底部即可
收缩:
1 private void close() { 2 RelativeLayout.LayoutParams mlayoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mActivity.getResources().getDimensionPixelSize(R.dimen.bottomlistheight)); 3 mlayoutparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 4 mrelativelayout.setLayoutParams(mlayoutparams); 5 mswitch = !mswitch; 6 }
收缩略有不同,需要设置好高度