Tôi đang phải đối mặt với nhiệm vụ dường như đơn giản của việc có một bố trí với:ConstraintLayout: nơi TextView dưới ImageView
- Một
ImageView
uống đầy đủ chiều rộng, chiều cao theo tỷ lệ khía cạnh hình ảnh của - một
TextView
dưới đây mà
Đây là những gì tôi có
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/hamster"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText "
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/image_view"/>
</android.support.constraint.ConstraintLayout>
Và đây là những gì nó trông giống như:
Như bạn có thể thấy, ImageView
có một biên độ lên đỉnh mà tôi không muốn.
Nếu tôi thay đổi ImageView's
trở ngại từ
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
để
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
(loại bỏ các hạn chế dưới), kết quả sẽ như thế này:
Bây giờ ImageView
là lần thứ e rất đầu của bố cục của tôi, nhưng TextView
đã biến mất. Tâm trí bạn, cho các biến thể thứ hai, xem trước bố cục Android Studio 3 cho thấy cách bố trí chính xác như tôi muốn nó:
Tôi đang làm gì sai và làm thế nào tôi có thể sửa chữa nó?
Lưu ý: Bố cục cần phải là ConstraintLayout
và chiều cao của bố cục phải là wrap_content
.
Bạn có thể tìm thấy dự án thử nghiệm của tôi here.
CHỈNH SỬA: Sự cố dường như chỉ xảy ra trên các thiết bị lớn hơn. Tôi đang sử dụng trình giả lập Nexus 9.
Cậu cố gắng sử dụng 'ứng dụng: layout_constraintVertical_bias = 0.0' –
@Ascorbin, công việc giải pháp của nirva shah của ?? –
@ShwetaChauhan Không, hãy xem bình luận của tôi bên dưới câu trả lời của anh ấy. – FWeigl