【问题标题】:Resizing height in Shape Drawable在 Shape Drawable 中调整高度
【发布时间】:2021-09-26 19:07:04
【问题描述】:

我有一个可绘制形状的资源文件。
就是下面的drawable.xml代码。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="-2dp"
          android:bottom="-2dp"
          android:top="-2dp">
        <shape android:shape="rectangle">
            <stroke android:width="@dimen/px02"
                    android:color="@color/white" />
        </shape>
    </item>
</layer-list>

这是布局xml代码。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray333"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!!!"
        android:textColor="@color/black"
        android:background="@drawable/bg_category_border"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

结果是这样的。

您可以看到左侧的白条。
我想让栏的大小更短,顶部和底部与 TextView 的文本一样高。(红线)
我试图在 drawable.xml 中给出填充和大小。
我在 TextView 上设置了 android:includeFontPadding。
但它没有用。
我该怎么做??

【问题讨论】:

    标签: android textview android-drawable android-shape


    【解决方案1】:

    我可以这样做

    从底部和顶部删除填充有点棘手,但您可以尝试以下 xml 并根据需要进行一些调整。

    bg_category_border.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item >
            <shape android:shape="rectangle">
                <stroke
                    android:width="1dp"
                    android:color="@color/white" />
            </shape>
        </item>
    </layer-list>
    

    布局 xml

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#3C3C3C"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:background="@drawable/bg_category_border"
            >
    
            <TextView
                android:textSize="50sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!!!"
                android:includeFontPadding="false"
                android:layout_marginBottom="-8dp"
                android:layout_marginTop="-10dp"
                android:textColor="@color/black"
                />
        </LinearLayout>
    
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • Margin 属性没有-dp。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多