9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

android.support.v7.widget.SearchViewで下線を消す

Last updated at Posted at 2015-05-28

概要

android.support.v7.widget.SearchViewを使って検索ボックスを設置した際に表示される下線を消したい時があります

f564fc371b4320be5ce822a0bc40bdf8.png

activity_main.xml
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray">
    <android.support.v7.widget.SearchView
        android:id="@+id/search_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:layout_margin="16dp"/>
</RelativeLayout>
MainActivity.java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SearchView searchView = (SearchView) findViewById(R.id.search_view);
        searchView.setIconified(false);

下線の消し方

  • SearchViewに背景色を設定しても、下線は消えません。また、SearchView自体には下線の表示を制御するxmlの指定等存在しません。
  • この線はSearchViewの内部を構成している要素の一つであるsearch_plateに背景色を追加することで消すことができます。背景色はtransparentでも可です。
MainActivity.java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SearchView searchView = (SearchView) findViewById(R.id.search_view);
        searchView.setIconified(false);

        /* 以下のコードを追加 */
        searchView.findViewById(android.support.v7.appcompat.R.id.search_plate)
                .setBackgroundColor(getResources().getColor(android.R.color.transparent));
    }
  • 下線を消すことが出来ました

969ca14dfa3823f4401eb5b7e1ffc932.png

9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?