2015-09-26 160 views
3

我想創建一個活動與不在ActionBar中的SearchView(實際上,我使用NoActionBar主題)。現在,我想這個搜索查看有圓角(不是的EditText實際搜索查看裏面,但搜索查看本身),android自定義searchView圓角

這樣的:Mockup

所有我可以管理是這樣的:actual SearchView

有人能告訴我我必須改變什麼嗎? XML活動的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clipChildren="false"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:background="@color/theBlue"> 

    <SearchView 
     android:id="@+id/search_view" 
     android:layout_width="1000dp" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/bg_white_rounded" 
     android:clickable="true"/> 
</RelativeLayout> 

可繪製/ bg_white_rounded.xml的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#ffffff"/> 
<corners android:radius="10dp"/> 
<padding android:left="0dp" 
    android:bottom="0dp" 
    android:right="0dp" 
    android:top="0dp"/> 
</shape> 

活性的代碼:

public class MainActivity extends AppCompatActivity { 

SearchView searchView; 

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

    // Get search view and modify it to our needs 
    searchView = (SearchView) findViewById(R.id.search_view); 
    searchView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      searchView.onActionViewExpanded(); 
     } 
    }); 
    //int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null); 
    //int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null); 
    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_badge", null, null); 
    View searchPlate = searchView.findViewById(searchPlateId); 
    //View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); 
    searchPlate.setBackgroundResource(R.drawable.bg_white_rounded); 
} 
} 

註釋行事我已經嘗試過但沒有工作。 謝謝!

回答

4

儘管拉詹KS(謝謝!)答案的作品,它不像設計模型100%(填充自然增加SearchView的大小,而不增加包含EditText的大小)。

我找到了解決辦法是這樣的: 在MainActivity.java,改變search_edit_frame,search_plate和search_bar:

int searchFrameId = searchView.getContext().getResources().getIdentifier("android:id/search_edit_frame", null, null); 
    View searchFrame = searchView.findViewById(searchFrameId); 
    searchFrame.setBackgroundResource(R.drawable.bg_white_rounded); 

    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null); 
    View searchPlate = findViewById(searchPlateId); 
    searchPlate.setBackgroundResource(R.drawable.bg_white_rounded); 

    int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null); 
    View searchBar = findViewById(searchBarId); 
    searchBar.setBackgroundResource(R.drawable.bg_white_rounded); 

這導致搜索查看看酷似樣機。

2

只要給padding = corner radius,因爲邊角按實際搜索查看後臺隱藏將其設置爲備份,所以填充使得招

繪製/ bg_white_rounded.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#ffffff"/> 
    <corners android:radius="8dp"/> 
    <padding android:left="8dp" 
     android:bottom="8dp" 
     android:right="8dp" 
     android:top="8dp"/> 
    </shape>