我想用SearchView创建一个不在ActionBar中的Activity(事实上,我使用NoActionBar主题)。现在我希望这个SearchView有圆角(不是实际SearchView中的EditText,而是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);
}
}
评论专栏是我已经尝试但没有奏效的事情。 谢谢!
答案 0 :(得分:5)
虽然rajan 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);
这导致SearchView看起来与模型完全一样。
答案 1 :(得分:4)
只需给padding = corner radius
,因为实际SearchView隐藏了角落背景会将其设置为后退,因此填充可以解决问题
<强>抽拉/ 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>
答案 2 :(得分:1)
我仅使用xml和androidx SearchView就能达到预期的结果
<androidx.appcompat.widget.SearchView
...
app:queryBackground="@drawable/bg_white_rounded"
app:submitBackground="@drawable/bg_white_rounded"
android:background="@drawable/bg_white_rounded"/>
答案 3 :(得分:0)
drawable/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="20dp"/>
<padding android:left="15dp"
android:bottom="15dp"
android:right="15dp"
android:top="15dp"/>
</shape>
和.xml
<SearchView
...
android:submitBackground="@drawable/bg_white_rounded"
android:background="@drawable/bg_white_rounded"
注意:在我的情况下,解决方案结合了上述答案。感谢@rajan.kali 和@A.Kazarovets