ConstraintLayout 中视图的最大可能角半径

问题描述 投票:0回答:1

是否有标准方法将CardView和TextView的角半径设置为

ConstraintLayout
中的最大可能值?

假设我有两个水平参考线

H10
10%
)和
H24
24%
),并且我创建了从
H10
H24
的TextView/CardView。现在如何才能使该 TextView/CardView 的角半径达到最大半径? (这里最大值是
(24-10)/2 = 7%

一旦我将定义高度

48dp
的按钮的角半径设置为
30dp
(而不是有理值
24dp
),它就变得非常丑陋。所以我想是否有一种方法可以自动将圆角半径设置为最大可能值? (在 xml 中而不是在代码中)

我尝试通过设置适当形状的视图背景来做到这一点,但我认为这与直接为 TextView/CardView 设置角半径相同:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff"/>

    <stroke android:width="1dp"
        android:color="#ff000000"
        />

    <corners 
        android:radius="100dp"/>
</shape>

我通常将以下内容用于高度为

wrap_content
的 TextView:(我不想使用
wrap_content
,因为我想使用
app:autoSizeTextType="uniform"
属性`)

<com.google.android.material.textfield.TextInputLayout

style="@style/myTextInputLayoutStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/passwordInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:maxLength="10"
                android:paddingVertical="0dp"
                android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>

哪里

<style name="myTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="boxCornerRadiusTopStart">24dp</item>
    <item name="boxCornerRadiusBottomStart">24dp</item>
    <item name="boxCornerRadiusBottomEnd">24dp</item>
    <item name="boxCornerRadiusTopEnd">24dp</item>
    <item name="boxStrokeWidth">1dp</item>
    <item name="hintAnimationEnabled">true</item>
</style>
android android-constraintlayout material-components-android rounded-corners cornerradius
1个回答
0
投票

我不确定所有视图,但以下内容就像材质组件的魅力:只需设置

app:shapeAppearanceOverlay="@style/ShapeAppearance.Material3.Corner.Full"

示例:

<com.google.android.material.button.MaterialButton
        android:id="@+id/materialCardView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:shapeAppearanceOverlay="@style/ShapeAppearance.Material3.Corner.Full"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHeight_percent="0.2"/>

© www.soinside.com 2019 - 2024. All rights reserved.