You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何制作仅左上和右下带圆角的Android搜索栏?

如何制作左上和右下带圆角的搜索栏?

嘿,我来帮你搞定这个问题~你当前的代码通过app:cardCornerRadius给CardView设置了统一的圆角,所以四个角都会变成圆角。要实现你想要的左上和右下单独圆角的效果,这里有两种实用方案:

方案一:自定义Drawable形状(兼容性好)

这种方法适配所有Android版本,步骤简单清晰:

  1. 在res/drawable目录下创建一个名为rounded_corner_left_top_right_bottom.xml的文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 设置和原CardView一致的背景色 -->
    <solid android:color="@*android:color/primary_material_dark" />
    <!-- 指定左上和右下为24dp圆角,其余角设为0 -->
    <corners
        android:topLeftRadius="24dp"
        android:bottomRightRadius="24dp"
        android:topRightRadius="0dp"
        android:bottomLeftRadius="0dp" />
</shape>
  1. 修改你的布局代码,去掉CardView的app:cardCornerRadius和app:cardBackgroundColor,替换为自定义背景:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@id/search_bar_container"
        android:background="@*android:color/primary_material_dark"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        app:cardElevation="50.0dip">

        <android.support.v7.widget.CardView
            android:id="@id/search_bar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/search_bar_margin"
            app:cardElevation="50.0dip"
            <!-- 替换为自定义圆角背景 -->
            android:background="@drawable/rounded_corner_left_top_right_bottom">

            <Toolbar
                android:id="@id/search_action_bar"
                android:theme="?android:attr/actionBarTheme"
                android:contentInsetStartWithNavigation="64dp"
                android:navigationIcon="@drawable/ic_search_24dp"
                android:navigationContentDescription="@string/search_menu"
                android:background="@color/background_floating_material_dark"
                android:layout_width="fill_parent"
                android:layout_height="@dimen/search_bar_height"
                android:titleTextAppearance="@style/TextAppearance.SearchBar"
                android:title="@string/search_menu" />
        </android.support.v7.widget.CardView>
    </FrameLayout>

    <FrameLayout
        android:id="@id/main_content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

方案二:使用AndroidX CardView的ShapeAppearance(简洁高效)

如果你的项目已经迁移到AndroidX,推荐用这种方式,通过样式单独指定圆角,代码更简洁:

  1. 在res/values/styles.xml中添加一个自定义样式:
<style name="CardViewRoundedCorners">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopLeft">24dp</item>
    <item name="cornerSizeBottomRight">24dp</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>
</style>
  1. 在CardView中引用这个样式,同时去掉原有的app:cardCornerRadius:
<android.support.v7.widget.CardView
    android:id="@id/search_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/search_bar_margin"
    app:cardElevation="50.0dip"
    app:cardBackgroundColor="@*android:color/primary_material_dark"
    <!-- 应用自定义圆角样式 -->
    app:shapeAppearanceOverlay="@style/CardViewRoundedCorners">

两种方案都能实现你想要的左上+右下圆角效果,方案一兼容性更强,方案二更符合现代Android开发的最佳实践~

内容的提问来源于stack exchange,提问作者Marcel Alexandru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:10:42