【问题标题】:How to apply shapeAppreanace in EditText and TextView using Material Design in Android如何在 Android 中使用 Material Design 在 EditText 和 TextView 中应用 shapeAppreanace
【发布时间】:2020-09-30 16:30:28
【问题描述】:

我想设计一个带圆角的TextViewEditText

对此有一个直接的解决方案。使用custom shape background。 但是由于 Material Design 1.1.0 引入了shapeAppearance 主题属性,以将不同的形状应用于角落,这对所有 Material 组件(如 MaterialButtonBottomSheetMaterialCardView 等)都适用。 但它不适用于EditTextTextView。我也尝试使用MaterialTextView,但它不起作用。这就是我为EditText 设置样式的方式,它也类似于TextView

<style name="ThemeOverlay.Chat" parent="AppTheme">
        <item name="editTextStyle">@style/Overlay.Chat.EditText</item>
    </style>


    <style name="Overlay.Chat.EditText" parent="Widget.AppCompat.EditText">
        <item name="shapeAppearanceOverlay">@style/ShapeAppearance.Overlay.FullRound</item>
    </style>

    <style name="ShapeAppearance.Overlay.FullRound" parent="ShapeAppearance.MaterialComponents.LargeComponent">
        <item name="cornerSize">50dp</item>
    </style>

【问题讨论】:

    标签: android android-edittext android-theme android-styles material-components-android


    【解决方案1】:

    您也可以将 Material Components Library 引入的 MaterialShapeDrawable 应用到 TextViewEditText
    在这种情况下,您不能在布局或样式中使用shapeAppearanceOverlay 属性,因为这些组件没有默认定义为MaterialButtonMaterialCardViewMaterialShapeDrawable
    但是您以编程方式应用相同的ShapeAppearence

    例如:

    <TextView
        android:id="@+id/textview"
        android:backgroundTint="@color/secondaryColor"
        ../>
    

    您可以通过编程方式使用以下内容:

    float radius = getResources().getDimension(R.dimen.default_corner_radius);
    TextView textView = findViewById(R.id.textview);
    

    用圆角定义ShapeAppearanceModel

    ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
            .toBuilder()
            .setAllCorners(CornerFamily.ROUNDED,radius)
            .build();
    

    用这个ShapeAppearanceModel创建一个MaterialShapeDrawable

    MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
    

    将此背景应用于您的视图:

    ViewCompat.setBackground(textView,shapeDrawable);
    

    您可以使用EditText 实现相同的行为(但在这种情况下您也可以使用TextInputLayout):

    在你的布局中定义:

        <EditText
            android:id="@+id/edittext"
            android:paddingLeft="4dp"
            android:drawableLeft="@drawable/ic_add_24px"
            android:drawableTint="@color/..."
            android:hint="@string/...."
            ..>
    

    然后申请MaterialShapeDrawable:

    EditText editText = findViewById(R.id.edittext);
    //Apply the rounded corners 
    ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
                    .toBuilder()
                    .setAllCorners(CornerFamily.ROUNDED,radius)
                    .build();
    
    MaterialShapeDrawable shapeDrawable = 
                new MaterialShapeDrawable(shapeAppearanceModel);
    //Fill the background color
    shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color....));
    //You can also apply a stroke
    shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color....));
    
    //Apply the shapeDrawable to the background.
    ViewCompat.setBackground(editText,shapeDrawable);
    

    如果您想使用样式中定义的ShapeAppareace,您可以使用 不同的ShapeAppearanceModel 构造函数。例如:

    ShapeAppearanceModel shapeAppearanceModel =
                ShapeAppearanceModel.builder( this,
                        R.style.ShapeAppearance_MaterialComponents_MediumComponent,
                        R.style.ShapeOverlay).build();
    

    与:

    <style name="ShapeOverlay">
        <item name="cornerSize">16dp</item>
    </style>
    

    【讨论】:

    • 感谢详细回答。但是我们如何从主题动态改变 shapeAppreance 呢?即从 attr/shapeAppreanceLarge 获取 shapeAppreance。假设我有两个在 shapeAppreance 中具有不同cornerSize 的主题(8dp 和 32dp)。上面的方法会如何变化?
    • @BurhanuddinRashid 检查更新的答案。 ShapeAppearanceModel 有不同的构造函数。
    【解决方案2】:

    使用 rounded_edittext.xml 创建可绘制资源文件

        <?xml version="1.0" encoding="utf-8"?>
    <!--  res/drawable/rounded_edittext.xml -->
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
     <solid android:color="#FFFFFF"/>
        <corners
         android:bottomRightRadius="15dp"
         android:bottomLeftRadius="15dp"
      android:topLeftRadius="15dp"
      android:topRightRadius="15dp"/>
    </shape>
    

    然后在你的editText中调用创建的drawable为andriod:background

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <EditText  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="5dip"
        android:background="@drawable/rounded_edittext" />
    </LinearLayout>
    

    我觉得你可以走了。

    【讨论】:

    • 我在问题本身中发布了此类解决方案的链接,这是一种更硬编码的方式。我想要更灵活的解决方案,它会在 Android 中使用 Material Design 主题随主题而变化。
    • 您是否看到此文档。 link。这可能会对您有所帮助。
    • 我试过了。它适用于其他材料组件。但不适用于 EditText 和 TextView
    猜你喜欢
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多