【问题标题】:How to change the SearchView icon position from left to right如何从左到右更改 SearchView 图标位置
【发布时间】:2019-12-12 04:30:25
【问题描述】:

我有以下包含SearchView 的布局,我希望它的图标位于右侧而不是默认左侧。我在这里查找了多个帖子及其各自的答案,但没有一个有效。关于如何做的任何想法?有没有我可以添加的xml 属性,还是必须以编程方式添加?

<?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 的回答实施结果:

预期结果:

【问题讨论】:

    标签: android android-layout searchview


    【解决方案1】:

    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;
                }
            }
    

    【讨论】:

    • 很抱歉没有回复,我会在第二天尝试您的解决方案并通知您。感谢您的回答
    • 查看我更新的答案,让我知道它是否有效!
    • 您好,很抱歉这么晚才回复。我必须在夏天工作,所以我没有时间做其他事情。现在我回来准备解决问题​​了,只是一个问题:上面的类是我活动中的子类还是只是活动本身?
    • 活动本身!
    • 这不起作用,它只是添加了一个图标作为 ContextMenu 图标(当然还添加了一些搜索功能)。我在帖子中添加了结果的屏幕截图
    猜你喜欢
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2020-02-07
    • 2016-12-18
    • 2012-12-31
    相关资源
    最近更新 更多