添加可折叠的SearchView操作栏,不带操作栏

时间:2013-10-22 07:23:02

标签: android actionbarsherlock searchview collapsable

我有一个标题布局如下所示。enter image description here

现在在按钮右侧的下一个上面我要添加此图像enter image description here并尝试使用可折叠SearchView而不使用操作栏意味着我想应用相同的操作栏搜索效果查看我的布局标题。 enter image description here

此外,我还要删除标题上方应用的名称和图标。

任何想法如何实现这个?

您的帮助/建议将不胜感激......

提前致谢...

编辑::

我到目前为止所尝试的是......

这是我的 header.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <RelativeLayout
        android:id="@+id/layout_header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/topbar"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

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

            <Button
                android:id="@+id/btnPrevious"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/previous" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv_headerText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center|center_vertical|fill_horizontal"
            android:text="Home"
            android:textColor="#FFFFFF"
            android:textSize="20dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btnLogout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@drawable/logout" />

        <ImageButton
            android:id="@+id/imgSearch"
            style="@style/Widget.Sherlock.ActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/btnLogout"
            android:src="@drawable/ic_search_inverse"
            android:title="Search" />

        <com.actionbarsherlock.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/tv_headerText"
            android:layout_marginTop="10dp"
            android:layout_toLeftOf="@+id/imgSearch"
            android:background="@drawable/ic_search_inverse"
            android:visibility="gone" />
    </RelativeLayout>

</merge>

search_menu.xml

<item
    android:id="@+id/action_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always"
    android:title="Search"/>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <!-- header Part -->

    <include layout="@layout/header" />

    <!-- header Part is over. -->

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp" >

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="None"
            android:textColor="@color/black"
            android:textSize="25dp"
            android:textStyle="bold"
            android:visibility="gone" />

        <ListView
            android:id="@+id/list"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_below="@+id/et_productSearch"
            android:cacheColorHint="#00000000"
            android:scrollbars="none" >
        </ListView>
    </RelativeLayout>

</LinearLayout>

在我的 Home.java

ImageView imgSearch;
SearchView searchView;

imgSearch=(ImageView)findViewById(R.id.imgSearch);
searchView = ((SearchView)findViewById(R.id.searchView));


searchView.setOnCloseListener(new SearchView.OnCloseListener() {
   @Override
   public boolean onClose() {
        searchView.setVisibility(View.INVISIBLE);
        return false;
        }
   });

我的 OnOptionItemSelected ::

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.action_search:
            if (searchView.getVisibility() == View.VISIBLE) {
                searchView.setIconified(true);
                searchView.setVisibility(View.INVISIBLE);
            } else {
                //searchView.setMaxWidth(searchView,getWidth() - imgSearch.getWidth());
                searchView.setVisibility(View.VISIBLE);
                searchView.setIconified(false);
            }
            break;
        }
        return true;
    }

1 个答案:

答案 0 :(得分:0)

这个问题已在SO中得到解答:

Android ActionBar: collapsible SearchView with action button

看看。