【问题标题】:android custom searchView rounded cornersandroid自定义searchView圆角
【发布时间】:2015-09-26 12:02:34
【问题描述】:

我想创建一个带有不在 ActionBar 中的 SearchView 的 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>

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="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);
}
}

注释行是我已经尝试过但没有用的东西。 谢谢!

【问题讨论】:

    标签: android searchview rounded-corners


    【解决方案1】:

    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

    【讨论】:

      【解决方案2】:

      我能够仅使用带有 androidx SearchView 的 xml 来实现所需的结果

      <androidx.appcompat.widget.SearchView
          ...
          app:queryBackground="@drawable/bg_white_rounded"
          app:submitBackground="@drawable/bg_white_rounded"
          android:background="@drawable/bg_white_rounded"/>
      

      【讨论】:

        【解决方案3】:

        只需给padding = corner radius,因为实际 SearchView 背景会将角落隐藏起来,所以填充是诀窍

        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="8dp"/>
            <padding android:left="8dp"
                android:bottom="8dp"
                android:right="8dp"
                android:top="8dp"/>
            </shape>
        

        【讨论】:

          【解决方案4】:

          虽然 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 看起来与模型完全相同。

          【讨论】:

            猜你喜欢
            • 2017-07-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多