简体   繁体   中英

Add ImageView on top of TextView (ConstraintLayout)

I didn't found any way to put an ImageView on top of TextView(s) with ConstraintLayout . It should look like on the screenshot below but if I run my app the imageview disappears.

在此处输入图片说明

Edit:

Adding android:translationZ="2dp" to ImageView doesn't solve the issue. However, here is my whole XML file:

<?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/line1"
        android:layout_width="160dp"
        android:layout_height="20dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorTableWhite"
        android:text="Lorem ipsum"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:textColor="@color/colorTableBlack" />

    <TextView
        android:id="@+id/line2"
        android:layout_width="160dp"
        android:layout_height="20dp"
        android:layout_marginStart="8dp"
        android:background="@color/colorTableWhite"
        android:text="Lorem ipsum"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/line1"
        tools:textColor="@color/colorTableBlack" />

    <TextView
        android:id="@+id/line3"
        android:layout_width="160dp"
        android:layout_height="20dp"
        android:layout_marginStart="8dp"
        android:background="@color/colorTableRed"
        android:text="Lorem ipsum"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/line2"
        tools:textColor="@color/colorTableWhite" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="160dp"
        android:translationZ="2dp"
        android:layout_height="60dp"
        android:layout_marginStart="8dp"
        android:foregroundGravity="top"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/space2"
        app:srcCompat="@drawable/ic_launcher_foreground" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:gravity="left"
        android:text="Lorem ipsum"
        android:textSize="20sp"
        app:layout_constraintStart_toEndOf="@+id/line1"
        app:layout_constraintTop_toBottomOf="@+id/space2" />

    <TextView
        android:id="@+id/description"
        android:layout_width="0dp"
        android:layout_height="36dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="Lorem ipsum dolor sit amet, consetetur sadipscing"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/line2"
        app:layout_constraintTop_toBottomOf="@+id/title" />

    <Space
        android:id="@+id/space2"
        android:layout_width="225dp"
        android:layout_height="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.426"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Space
        android:id="@+id/space"
        android:layout_width="225dp"
        android:layout_height="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/description" />

</android.support.constraint.ConstraintLayout>

Since you're using app:srcCompat , so either change ImageView to AppCompatImageView in your XML layout:

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/imageView"
    android:layout_width="160dp"
    android:translationZ="2dp"
    android:layout_height="60dp"
    android:layout_marginStart="8dp"
    android:foregroundGravity="top"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/space2"
    app:srcCompat="@drawable/ic_launcher_foreground" />

Or change app:srcCompat to android:src :

<ImageView
    android:id="@+id/imageView"
    android:layout_width="160dp"
    android:layout_height="60dp"
    android:layout_marginStart="8dp"
    android:foregroundGravity="top"
    android:translationZ="2dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/space2"
    android:src="@drawable/ic_warning" />

I found two solution for this issue:

  1. Your minSdk=24 so no need to use srcCompat. Try to change this:

app:srcCompat="@drawable/ic_warning"

to this:

android:background="@drawable/ic_warning"

  1. If you prefer to use app:srcCompat, then you should make a little modification to your getView method in TemplateRow.class. Below, modified version of getView() method:

     public View getView(int position, View convertView, ViewGroup parent) { // Get the data item for this position DisplayTemplate displayTemplate = getItem(position); // Check if an existing view is being reused, otherwise inflate the view ViewHolder viewHolder; // view lookup cache stored in tag final View result; if (convertView == null) { viewHolder = new ViewHolder(); LayoutInflater inflater = LayoutInflater.from(getContext()); convertView = inflater.inflate(R.layout.template, parent, false); viewHolder.line1 = convertView.findViewById(R.id.line1); viewHolder.line2 = convertView.findViewById(R.id.line2); viewHolder.line3 = convertView.findViewById(R.id.line3); viewHolder.title = convertView.findViewById(R.id.title); viewHolder.description = convertView.findViewById(R.id.description); viewHolder.imageView = convertView.findViewById(R.id.imageView); result = convertView; convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); result = convertView; } Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top); result.startAnimation(animation); lastPosition = position; viewHolder.title.setText(displayTemplate.getTitle()); viewHolder.description.setText(displayTemplate.getDescription()); Drawable drawable = parent.getResources().getDrawable(R.drawable.ic_warning, null); viewHolder.imageView.setImageDrawable(drawable); showModel(viewHolder.line1,displayTemplate.getLines().get(0)); showModel(viewHolder.line2,displayTemplate.getLines().get(1)); showModel(viewHolder.line3,displayTemplate.getLines().get(2)); // Return the completed view to render on screen return convertView;} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM