android expandable menu (hiding/unhiding menu)

*爱你&永不变心* 提交于 2020-01-15 09:39:46

问题


in my application layout, i have a setting button....when we push it, it will show some sub menus....when we push it again, it will hide all sub menus....look like expandable listview, but not exactly like that....just hiding/unhiding menu in order to get some space for another views....how can i do that?

here is the screenshots that exactly what i want to do

hide menu

unhide menu

here is my xml file:

<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
    <EditText
        android:id="@+id/Editsearch"
        android:hint="Enter keyword..."
        android:layout_alignParentLeft="true"
        android:singleLine="true"
        android:layout_width="fill_parent"
        android:layout_toLeftOf="@+id/search"
        android:layout_height="wrap_content" />
    <ImageButton
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_search"
        android:layout_alignParentRight="true" />
    <Spinner
        android:id="@+id/province"
        android:layout_width="135dp"
        android:layout_height="45dp"
        android:layout_below="@+id/Editsearch"
        android:layout_toLeftOf="@+id/setting"
        android:textSize="12dip"
        android:textStyle="bold"
        android:background="@android:drawable/btn_default"
    />
    <ImageButton
        android:id="@+id/setting"
        android:layout_width="wrap_content"
        android:layout_height="45dip"
        android:layout_below="@+id/Editsearch"
        android:src="@drawable/button_setting"
        android:layout_alignParentRight="true" />
    <Spinner
        android:id="@+id/Category"
        android:layout_width="135dp"
        android:layout_height="45dp"
        android:text="Category"
        android:layout_below="@+id/Editsearch"
        android:layout_alignParentLeft="true"
        android:background="@android:drawable/btn_default" />
        <!-- Job Category title-->
    <TextView
        android:id="@+id/Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:typeface="serif"
        android:textSize="13dip"
        android:layout_below="@+id/province"
        android:layout_gravity="center"

        android:text="Total Job : 1982"
        android:textStyle="bold" />
    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Title"
        android:listSelector="@drawable/list_selector" />
</RelativeLayout>

thank in advance


回答1:


It is very simple. Just place those 3 buttons in a layout and set the visibility of that layout to gone/visible on click of hide/unhide button.

EDIT :

It will be like put your spinners which you like to hide/unhide in a layout and provide an ID for it.

<ImageButton
        android:id="@+id/setting"
        android:layout_width="wrap_content"
        android:layout_height="45dip"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/Editsearch"
        android:src="@drawable/button_setting" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    **android:id="@+id/submenu_layout"**
    android:layout_below="@+id/Editsearch"
    >
    <Spinner
        android:id="@+id/category1"
        android:layout_width="135dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:background="@android:drawable/btn_default"
        android:text="Category" />
    <Spinner
        android:id="@+id/category2"
        android:layout_width="135dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/category1"
        android:background="@android:drawable/btn_default"
        android:text="Category" />
    <Spinner
        android:id="@+id/category3"
        android:layout_width="135dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/category2"
        android:background="@android:drawable/btn_default"
        android:text="Category" />
    </RelativeLayout>

Then use this id to take the reference of this layout in your code.

RelativeLayout submenuLayout = (RelativeLayout) findViewById(R.id.submenu_layout);

EDIT 2:

After that OnClick of setting imageButton :

 if(submenuLayout .getVisibility() == View.VISIBLE)
    submenuLayout .setVisibility(View.GONE);
    else
        submenuLayout .setVisibility(View.VISIBLE);



回答2:


use this code

to make it visible on button click

themeUrlRow.setVisibility(View.VISIBLE);

to make it invisible

themeUrlRow.setVisibility(View.GONE);

and in xml put the button in a layout and also make it invisible and in on clicklistener add the above code.

xml code

            <LinearLayout
               android:id="@+id/themeUrlRow"
            android:layout_width="fill_parent"
            android:gravity="center"
            android:visibility="gone" > />

            <Button
                android:id="@+id/register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Register" />

            <Button
                android:id="@+id/register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Register" />
        </LinearLayout>


来源:https://stackoverflow.com/questions/14725966/android-expandable-menu-hiding-unhiding-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!