无法动态显示cardView

时间:2019-05-11 15:43:25

标签: java android layout

我想动态显示班级卡,但是它不能在屏幕上显示。

我添加一个LinearLayout并将其设置为params,然后将其添加到Layout中。

        private void createItemcourseView(final Course course) {
    int dayId = 0;
   //switch()
    day = (RelativeLayout)findViewById(dayId);
    int height = 180;
    View v = LayoutInflater.from(this).inflate(R.layout.class_card_layout, null); //load the classcard
    v.setY(height * (course.getStart() - 1)); //set the height of the class
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
            (ViewGroup.LayoutParams.MATCH_PARENT, (course.getEnd() - course.getStart() + 1) * height - 8); //set the layout height
    v.setLayoutParams(params);
    TextView text = v.findViewById(R.id.text_view);
    text.setText(course.getCourse() + "\n" + course.getTeacher() + "\n" + course.getClassroom()); //show the course name
    day.addView(v);

    //delete the class with long time press
    v.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            v.setVisibility(View.GONE);
            day.removeView(v);
            LitePal.delete(Course.class, course.getmainId());
            return true;
        }
    });
}

layout.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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="45dp"
android:layout_height="70dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="#7feacdd1"
app:cardCornerRadius="7dp"
android:id="@+id/card_view">

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"/>

</android.support.v7.widget.CardView>`

我使用了litepal数据库,并创建了一个名为课程的类

0 个答案:

没有答案