SearchView窗口小部件文本未与工具栏的左/左对齐

时间:2018-10-23 11:04:26

标签: android search material-design searchview

enter image description here我正在SearchView中实现Activity。一切正常,但文本开始或对齐到中心而不是向左对齐。

下面是xml代码

search_toolbar.xml

<item
    android:id="@+id/m_search"
    android:icon="@drawable/ic_search"
    android:title=""
    android:visible="true"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView" />

在主要活动中,我定义了回调,这些回调用于片段中以基于查询刷新数据。

主要活动代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_toolbar, menu);
    MenuItem item = menu.findItem(R.id.m_search);
    MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Called when SearchView is collapsing
            if (item.isActionViewExpanded()) {
                animateSearchToolbar(1, false, false);
                toolbar_title.setVisibility(View.VISIBLE);
            }
            return true;
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Called when SearchView is expanding
            toolbar_title.setVisibility(View.GONE);
            animateSearchToolbar(1, true, true);
            return true;
        }
    });
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.m_search));
    searchView.setGravity(Gravity.START);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.w(TAG, "onQueryTextSubmit : " + query);
            if (!TextUtils.isEmpty(query)) {
                callback.searchQuery(query);
            }
            hideKeyboard();
            return true;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            Log.w(TAG, "onQueryTextChange : " + s);
            if (TextUtils.isEmpty(s)) {
                hideKeyboard();
                callback.refreshData();
            }
            return false;
        }
    });
    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            Log.w(TAG, "OnCloseListener : ");
            return false;
        }
    });
    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.w(TAG, "OnSearchClickListener : ");
        }
    });
    return true;
}

这是main_activity.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/appBarLayout"
    android:background="@android:color/white"
    android:gravity="center">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:layout_collapseMode="parallax"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <tlb.com.customviews.TextViewMedium
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="@string/title"
                android:textAllCaps="true"
                android:textColor="@color/button_color"
                android:textSize="@dimen/text_size_18"/>

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomNavigationView"
        android:background="@android:color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        app:itemBackground="@drawable/bottom_navigation_colors"
        app:itemIconSize="@dimen/bottom_navigation_icon_size"
        app:itemTextAppearance="@style/menu_text_style"
        app:menu="@menu/bottom_navigation_menu" />

</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/activity_main_drawer" />

1 个答案:

答案 0 :(得分:0)

尝试更改

searchView.setGravity(Gravity.START);

对于

searchView.setGravity(Gravity.LEFT);