在 Shape Drawable 中调整高度

问题描述

我有一个形状可绘制资源文件
这是下面的 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>

结果就是这样。

enter image description here

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

解决方法

我可以这样做

enter image description here

从底部和顶部移除填充有点棘手,但您可以尝试以下 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>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...