Gre*_*nis 2 android android-constraintlayout
我想创建这个:
并且可以使用此LinearLayout执行此操作:
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<ImageView android:id="@+id/image"
android:src="@drawable/image"
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="48sp"
android:text="Hello World" />
</LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
哪个效果很好.现在,我正在尝试使用ConstraintLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<ImageView android:id="@+id/image"
android:src="@drawable/image"
android:adjustViewBounds="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48sp"
android:text="Hello World"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/image" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会产
底部被切断了.问题是ConstraintLayout没有正确调整它的高度来尝试尊重adjustViewBounds图像.我已经看到了使用的建议,layout_constraintHeight_default并尝试过:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<ImageView android:id="@+id/image"
android:src="@drawable/image"
android:adjustViewBounds="true"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48sp"
android:text="Hello World"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/image" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会产
因此,似乎使用了layout_constraintHeight_default破坏ImageView的adjustViewBounds.
那么,怎么做呢?
注意:我在RecyclerView中使用ConstraintLayout并且必须使用高度wrap_content.
我试着自己重现你的问题.我发现当我使用constraint-layout库的1.0.2版本时,我遇到了同样的问题,但是当我使用版本1.1.0-beta4时,我没有.您可以将gradle文件中的依赖项更改为
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta4'
Run Code Online (Sandbox Code Playgroud)
请注意,1.0.2和1.1.0-beta4之间存在一些非必要向后兼容的变化; 看到这个问题,围绕图书馆改变的一种方式进行了一些讨论.