2017-11-08 18 views
6

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ư:

enter image description here

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:

enter image description here

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ó:

enter image description here

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.

+1

Cậu cố gắng sử dụng 'ứng dụng: layout_constraintVertical_bias = 0.0' –

+0

@Ascorbin, công việc giải pháp của nirva shah của ?? –

+0

@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

Trả lời

2

Có như Akash Amin cho biết thêm hai dòng này trong ImageView bạn

app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintVertical_bias="0.0" 
+0

Việc thêm tính năng này vào ImageView đã khắc phục được sự cố, cảm ơn. – FWeigl

+0

vui lòng giúp bạn. –

0

Hãy thử điều này đó là hữu ích cho bạn


<?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"> 


    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:srcCompat="@color/colorAccent" 
     app:layout_constraintBottom_toTopOf="@+id/textView2"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="0dp" 
     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 " 
     android:textAlignment="center" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/imageView2" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" /> 
</android.support.constraint.ConstraintLayout> 

enter image description here

+0

Kết quả của việc này là http://i66.tinypic.com/24nps1e.jpg, thật không may là điều tôi muốn. – FWeigl

0

Thử điều này ..

<?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:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@mipmap/ic_launcher" 
     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_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@id/image_view" /> 

</android.support.constraint.ConstraintLayout> 

Xóa bootom contraint của ImageView và thiết lập TextView hạn chế trên xuống dưới của ImageView

+0

Thật không may, vấn đề vẫn tồn tại với xml của bạn trên các thiết bị lớn hơn. Tôi đã chỉnh sửa câu hỏi của mình. – FWeigl

0
<?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="match_parent"> 

    <ImageView 
     android:id="@+id/image_view" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="0dp" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/ic_launcher_background" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="@id/guideline" /> 

    <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 t 
     MyText MyText MyText MyText " 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@id/guideline" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.45" /> 

</android.support.constraint.ConstraintLayout> 
+0

Bạn nên sử dụng Ctrl + K hoặc Cmd + K để định dạng đúng các đoạn mã nguồn của bạn. –

Các vấn đề liên quan