【发布时间】:2017-05-20 05:32:41
【问题描述】:
我使用了constraintLayout 和layout_constraintDimensionRatio="1:1"
(宽度为wrap_content,高度为0dp (match_constraint))
因此,我预计宽度和高度为 1:1,但它不起作用。
怎么了?
我附上了代码和截图。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@android:color/holo_blue_bright"
android:gravity="center"
android:text="Hello World!11"
app:layout_constraintDimensionRatio="1:1" />
</android.support.constraint.ConstraintLayout>
我引用了有关 Constraintlayout 的 android 开发者网站。 https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints
Ratio :: 您还可以将小部件的一维定义为 另一个。为了做到这一点,你需要至少有一个 将约束维度设置为 0dp(即 MATCH_CONSTRAINT),并设置 属性 layout_constraintDimentionRatio 到给定的比率。为了 示例:
<Button android:layout_width="wrap_content" android:layout_height="0dp" app:layout_constraintDimensionRatio="1:1" /> will set the height of the button to be the same as its width.
但它不起作用。
【问题讨论】:
-
ConstrainLayout的版本是多少? -
嗨,Shailesh。我用的是 1.0.2
-
match_parent不受支持。你需要设置0dp所以ConstraintLayout相应地设置它。检查这个stackoverflow.com/questions/37603751/… -
如果您想创建,那么您可以通过其他方式进行。检查这个stackoverflow.com/questions/37318228/…
-
@Shailesh 谢谢评论。我知道不支持 match_parent。但它仅适用于直接在 ConstraintLayout 下的视图(不确定..)
标签: android android-constraintlayout