0

我正在尝试自动设置 TextView 样式。我设法设置了文本外观,但不知何故只忽略了顶部边距和底部边距。

<style name="TextViewStyle" parent="@android:style/Widget.TextView">
    <item name="android:textColor">?android:textColorPrimary</item>
    <item name="android:textColorLink">?colorPrimary</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
</style>

<style name="TextViewStyle.Medium">
    <item name="android:textSize">@dimen/text_medium</item>
</style>

<style name="TextViewStyle.Medium.Normal">
    <item name="android:fontFamily">@font/open_sans</item>
</style>

<style name="main_steps_with_icon" parent="TextViewStyle.Medium.Normal">
    <item name="android:layout_marginStart">@dimen/margin_big</item> // 24dp
    <item name="android:layout_marginTop">@dimen/margin_normal</item> // 16dp
    <item name="android:layout_marginEnd">@dimen/margin_big</item> // 24dp
    <item name="android:drawableStart">@drawable/ic_baseline_verified_user_24</item>
    <item name="android:drawablePadding">@dimen/margin_big</item> // 24dp
</style>

在代码中:

val v = TextView(stepsGroup.context, null, 0, R.style.main_steps_with_icon)
v.layoutParams = applyLayoutParams(v, R.style.main_steps_with_icon)
v.text = steps[i].message
stepsGroup.addView(v)

appkyLayoutParams 方法:

private fun applyLayoutParams(
    v: View,
    styleResId: Int
): LinearLayout.LayoutParams {
    val attrs = intArrayOf(
        android.R.attr.layout_width,
        android.R.attr.layout_height,
        android.R.attr.layout_marginStart,
        android.R.attr.layout_marginTop,
        android.R.attr.layout_marginEnd,
        android.R.attr.layout_marginBottom,
    )

    val arr: TypedArray = v.context.obtainStyledAttributes(styleResId, attrs)

    val layoutWidth = arr.getLayoutDimension(0, LinearLayout.LayoutParams.MATCH_PARENT)
    Log.i(TAG, "layoutWidth: $layoutWidth")

    val layoutHeight = arr.getLayoutDimension(1, LinearLayout.LayoutParams.WRAP_CONTENT)
    Log.i(TAG, "layoutHeight: $layoutHeight")

    val layoutParams = LinearLayout.LayoutParams(
        layoutWidth,
        layoutHeight
    )

    val marginStart = arr.getDimensionPixelSize(2, 0)
    Log.i(TAG, "marginStart: " + (marginStart / v.resources.displayMetrics.density) + "dp")

    val marginTop = arr.getDimensionPixelSize(3, 0)
    Log.i(TAG, "marginTop: " + (marginTop / v.resources.displayMetrics.density) + "dp")

    val marginEnd = arr.getDimensionPixelSize(4, 0)
    Log.i(TAG, "marginEnd: " + (marginEnd / v.resources.displayMetrics.density) + "dp")

    val marginBottom = arr.getDimensionPixelSize(5, 0)
    Log.i(TAG, "marginBottom: " + (marginBottom / v.resources.displayMetrics.density) + "dp")

    @Suppress("UNUSED_CHANGED_VALUE")
    layoutParams.setMargins(
        marginStart,
        marginTop,
        marginEnd,
        marginBottom,
    )

    arr.recycle()

    return layoutParams
}

日志猫:

I/ViewUtils: layoutWidth: -1
I/ViewUtils: layoutHeight: -2
I/ViewUtils: marginStart: 24.0dp
I/ViewUtils: marginTop: 0.0dp
I/ViewUtils: marginEnd: 24.0dp
I/ViewUtils: marginBottom: 0.0dp

我错过了什么吗?

4

0 回答 0