Horizontal Menus

2019-04-29 21:15发布

问题:

I want to create horizontal menu in android. Android supports expandable list view that expands vertically but i want to expand menus horizontally. Please refer image

Description:

In the images Menu1,menu2,menu3 are main menus and s1,s2,s3 are sub items of menu 1. If i clicked on main menu its sub items must be expand.

回答1:

You can put submenus in an LinearLayout and add play with View.VISIBLE/View.GONE in onClickListener



回答2:

It's a simple example. you should complete it by yourself.

in xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/attachments_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/btn_menu1"
        android:layout_width="0dp"
        android:layout_height="50dp" 
        android:text="Menu1"
        android:layout_weight="1"
        />
    <LinearLayout
        android:id="@+id/subview_menu1"
        android:layout_width="0dp"
        android:layout_height="50dp" 
        android:layout_weight="2"
        android:visibility="gone"
        >
        <Button
            android:layout_width="0dp"
            android:layout_height="fill_parent" 
            android:text="S1"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="fill_parent" 
            android:text="S2"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="fill_parent" 
            android:text="S3"
            android:layout_weight="1"
            />
    </LinearLayout>
    <Button
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:text="Menu2" 
        android:layout_weight="1"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:text="Menu3" 
        android:layout_weight="1"
        />
    </LinearLayout>

in btn_menu1 OnClickListener

public void onClick(View v) {
    if (subview_menu1.isShown()) {
        subview_menu1.setVisibility(View.GONE);
    }
    else{
        subview_menu1.setVisibility(View.VISIBLE);
    }
}