【问题标题】:Nine Patch With Constraint Layout九个带约束布局的补丁
【发布时间】:2018-05-29 01:09:35
【问题描述】:

是否可以使用带有约束布局的 9-patch 可绘制对象?我正在尝试渲染可编辑的分数。两个edittext由九个补丁drawable分隔,基本上是一条可以水平扩展的黑线:

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraint_content"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/numerator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:hint="@string/x"
        android:inputType="number"
        android:textColorHint="@android:color/transparent"
        android:textSize="24sp"
        mr:layout_constraintBottom_toTopOf="@id/denominator" />

    <EditText
        android:id="@+id/denominator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:hint="@string/x"
        android:inputType="number"
        android:textColorHint="@android:color/transparent"
        android:textSize="24sp"
        mr:layout_constraintBottom_toBottomOf="@id/numerator" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/fraction"
        mr:layout_constraintEnd_toEndOf="@id/denominator"
        mr:layout_constraintStart_toStartOf="@string/numerator" />

</android.support.constraint.ConstraintLayout>

当分子或分母增加长度时,线条被正确渲染,但它本身并没有拉伸。 drawable 在“经典”布局(LinearLayout)下工作正常。

9 patch drawable的链接

编辑:添加:

mr:layout_constraintWidth_default="percent"
mr:layout_constraintWidth_percent="1"

查看布局似乎有效,现在我需要将内容居中...

【问题讨论】:

  • 可以和any一起使用View
  • 好的,你能告诉我我哪里错了吗?
  • 贴两张图片:当你的“长度”正常时和增加时
  • @pskink我已按要求进行了编辑。
  • 好的,现在我看到了,阅读developer.android.com/guide/topics/graphics/…。注意“可拉伸区域”

标签: android android-constraintlayout nine-patch


【解决方案1】:

用于显示九个补丁可绘制对象的View 的约束之一存在错误:

mr:layout_constraintStart_toStartOf="@string/numerator"

您试图将 View 的结尾限制为字符串资源,这实际上不会引发任何错误。

由于您希望此 View 与提名者或分母(以较长者为准)一起扩展,并且您的父 ConstraintLayoutandroid:layout_width 设置为 wrap_content 您可以约束 View 的水平约束像这样给parent

<View
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@drawable/fraction"
    mr:layout_constraintBottom_toTopOf="@id/denominator"
    mr:layout_constraintEnd_toEndOf="parent"
    mr:layout_constraintStart_toStartOf="parent"
    mr:layout_constraintTop_toBottomOf="@id/numerator" />

结果:

【讨论】:

    猜你喜欢
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多