如何从左到右更改SearchView图标的位置 [英] How to change the SearchView icon position from left to right
本文介绍了如何从左到右更改SearchView图标的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下包含SearchView
的布局,并且我希望其图标位于右侧而不是默认的左侧.我在此处查找了多个帖子及其各自的答案,但没有一个起作用.关于如何做的任何想法?我可以添加xml
属性吗,还是必须以编程方式进行设置?
I have the following layout which contains a SearchView
and i want its icon to be on the right side instead of the default left. I looked up multiple posts on here and their respective answers but none worked. Any ideas on how to do it? Is there an xml
attribute i can add or do i have to do it programatically?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presenter.CultureActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/culture_toolbar"
android:layout_width="match_parent"
android:layout_height="145dp"
android:background="@drawable/toolbar_gradient"
android:elevation="4dp"
android:paddingTop="40dp"
app:layout_scrollFlags="scroll|enterAlways"
app:titleTextColor="@android:color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="20dp">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="10dp"
android:fontFamily="@font/montserrat_medium"
android:text="@string/toolbar_title_placeholder"
android:textColor="@android:color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.SearchView
android:id="@+id/searchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="5dp"
android:background="@drawable/searchbar_rounded_bg"
app:defaultQueryHint="@string/search_hint"
app:iconifiedByDefault="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar_title" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
<!-- Implement the main layout -->
<include
android:id="@+id/shadow"
layout="@layout/navbar_shadow"
android:layout_width="match_parent"
android:layout_height="4dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<include
android:id="@+id/bottom_nav_bar"
layout="@layout/bottom_navbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Sumit Shukla的答案实现的结果:
Result of Sumit Shukla's answer implementation:
预期结果:
推荐答案
menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_white"
android:title="@string/search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu
YourActivity.java
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
//Override below methods:
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
这篇关于如何从左到右更改SearchView图标的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文