创建适配器时,布局未正确显示

时间:2016-10-12 07:28:33

标签: android android-studio

代码

  listView.setAdapter(new BaseAdapter() {
        @Override
        public int getCount() {
            return 10;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView=getActivity().getLayoutInflater().inflate(R.layout.fragment_my_auto_adap,parent,false);
            return convertView;
        }
    });

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          xmlns:app="http://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:layout_height="100dp"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="10dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:id="@+id/cardView"
    android:layout_width="match_parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="64dp"
            android:layout_height="64dp"
            app:srcCompat="@drawable/car_item_r"
            android:id="@+id/imageView"
            android:adjustViewBounds="true"
            android:layout_marginStart="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_height="0dp"
            android:id="@+id/line"
            android:layout_marginStart="8dp"
            app:layout_constraintLeft_toRightOf="@+id/imageView"
            android:layout_marginLeft="8dp"
            app:layout_constraintTop_toTopOf="@+id/imageView"
            app:layout_constraintBottom_toBottomOf="@+id/imageView"
            android:background="#F38C8D"
            android:layout_width="1dp"/>

        <TextView
            android:text="Nisan Teana"
            android:layout_width="wrap_content"
            android:layout_height="17dp"
            android:id="@+id/textViewName"
            app:layout_constraintBottom_toBottomOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="@+id/imageView"
            app:layout_constraintLeft_toRightOf="@+id/line"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"/>

        <ImageView
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/chat"
            android:id="@+id/imageView3"
            android:adjustViewBounds="true"
            tools:layout_editor_absoluteY="16dp"
            android:layout_marginEnd="32dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginRight="32dp"
            android:layout_width="48dp"/>

        <TextView
            android:text="Лайкнуть кампанию"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:textSize="8sp"
            app:layout_constraintTop_toBottomOf="@+id/imageView3"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="16dp"
            app:layout_constraintRight_toRightOf="@+id/imageView3"
            app:layout_constraintLeft_toLeftOf="@+id/imageView3"/>

    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

结果

enter image description here

布局未正确显示 我该怎么做才能正常工作?

如果我使用RelativeLayout,一切正常。
怎么了?我不明白。

1 个答案:

答案 0 :(得分:0)

Android Studio在ImageView上为您提供了明确可理解的错误消息: enter image description here

所以,添加一个垂直约束。

app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"