您的位置:首页 > 其它

如果自定义一个可以点击之后展开/收缩的菜单组件

2015-02-28 21:53 295 查看
首先,定义布局文件,如下

<?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方法了,将其设置为在顶部布局的底部即可

收缩:

private void close() {
RelativeLayout.LayoutParams mlayoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, mActivity.getResources().getDimensionPixelSize(R.dimen.bottomlistheight));
mlayoutparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mrelativelayout.setLayoutParams(mlayoutparams);
mswitch = !mswitch;
}


收缩略有不同,需要设置好高度
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐