簡體   English   中英

如何使SearchView小部件中的搜索圖標與布局的右端對齊?

[英]How do i make the search icon in the SearchView widget to align to the right end of the layout?

如何使SearchView小部件中的搜索圖標與布局的右端對齊? 當前它顯示在最左端。 在此處輸入圖片說明

在主要活動xml布局中,我有

  <SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:elevation="2dp"
            android:background="#fff"
            android:layout_height="wrap_content"
            >
</SearchView>

主要活動

public class MainActivity extends AppCompatActivity {

    SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        searchView=(SearchView) findViewById(R.id.searchView);
        searchView.setQueryHint("Search View");


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(getBaseContext(), query, Toast.LENGTH_LONG).show();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                Toast.makeText(getBaseContext(), newText, Toast.LENGTH_LONG).show();
                return false;
            }
        });
    }

您可以通過這種方式以編程方式進行操作

SearchView searchView= (SearchView) item.getActionView();
searchView.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT)); 

或在Xml中,您需要更改視圖層次結構

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM