问题
Here is my layout, using android.support.v7.widget.SearchView
.
I want to move the search icon to the right side,but I haven't found any method...
`
<android.support.design.widget.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="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/AppTheme.PopupOverlay">
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tab_main_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
`
after i click the icon,it will change to this layout
回答1:
You can do it programmatically
SearchView search = (SearchView) item.getActionView();
search.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
回答2:
You can add search action in menu.xml like below:
<menu 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">
<item
android:id="@+id/action_search"
android:icon="@mipmap/ic_search_white_24dp"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<item android:id="@+id/action_download"
android:title="@string/menu_action_download"
android:icon="@mipmap/ic_file_download_white_24dp"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>
回答3:
<android.support.design.widget.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="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/AppTheme.PopupOverlay">
<RelativeLayout
android:id="@+id/not"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<android.support.v7.widget.SearchView
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tab_main_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
回答4:
you could try the layoutdirection. set it to "rtl"
android:layoutDirection="rtl" in your searchview xml:
<android.support.v7.widget.SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="11.4dp"
app:iconifiedByDefault="true"
android:animateLayoutChanges="true"
app:queryHint="@string/search_hint"
app:queryBackground="@android:color/transparent"
app:searchHintIcon="@drawable/empty_drawable"
app:closeIcon="@drawable/close_x_icon"
**android:layoutDirection="rtl"**
app:searchIcon="@drawable/magnifying_glass"
/>
dont forget to put this in your manifest application tag:
android:supportsRtl="true"
回答5:
Try this...It looks as you expect.Just add this line inside of SearchView Tag.
android:gravity="right"
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/AppTheme.PopupOverlay">
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tab_main_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
回答6:
Setting the SearchView
gravity to the right through xml or programmatically works but when the SearchView
was expanded then it was taking a small area of the width on the right which wasn't the intended look i had in mind, i wanted it to take the full width.
Note 1 : This solution didn't allow the title to show so i added a TextView
to hold the title.
Note 2 : When the SearchView
was expanded, it'd overlap on the custom title so i made the SearchView
background as the Toolbar to avoid keep hiding/showing the title with SearchView
's expand/collapse
Here is the Toolbar
code:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Toolbar Title"
android:textColor="@android:color/white" />
<android.support.v7.widget.SearchView
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="?attr/colorPrimary"
android:gravity="right" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
Inside the activity, i used the Close and SearchClick listeners to align the SearchView
to the left.
mSearchView = findViewById(R.id.search_view);
mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) mSearchView.getLayoutParams();
param.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
//set layout params to cause layout update
mSearchView.setLayoutParams(param);
return false;
}
});
mSearchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) mSearchView.getLayoutParams();
param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//set layout params to cause layout update
mSearchView.setLayoutParams(param);
}
});
回答7:
Probably too late for the answer but this worked for me, as you can see from the screenshot attached.
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintBottom_toTopOf="@+id/relativeLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Contacts">
<android.support.v7.widget.SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:gravity="center_vertical"
app:queryHint="Search" />
</android.support.v7.widget.Toolbar>
回答8:
In case you want to use SearchView
outside of ActionBar
in any other view (let's say for example ConstraintLayout
) you can use the approach posted on this site. Basically the icon is in an ImageView
positioned at the start of layout (or I could say "it is the first element added to its parent"). With this code, you are searching for it, then remove it from the parent, and finally put it back. The result: it will be positioned at the "end" of the SearchView
widget... problem solved...
//Get ImageView of icon
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
//Get parent of gathered icon
ViewGroup linearLayoutSearchView = (ViewGroup) searchViewIcon.getParent();
//Remove it from the left...
linearLayoutSearchView.removeView(searchViewIcon);
//then put it back (to the right by default)
linearLayoutSearchView.addView(searchViewIcon);
Tested, working, satisfied... ;)
来源:https://stackoverflow.com/questions/36905036/how-to-move-searchview-s-search-icon-to-the-right-side