概要
android.support.v7.widget.SearchViewを使って検索ボックスを設置した際に表示される下線を消したい時があります
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));
}
- 下線を消すことが出来ました