Android片段中未显示网格布局

时间:2019-09-16 06:43:16

标签: android android-layout android-fragments

我想创建一些具有约束布局作为根布局的4网格布局。该文件被创建为一个片段,但是我认为这不是问题,标题中提到了该文件以尽可能详细地描述它。

xml文件如下所示:

<?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"
    android:background="@color/colorPrimaryLight"
    tools:context=".view.LoanDetail">

<View
    android:id="@+id/divider_loan"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/line_white"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:weightSum="2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/divider_loan">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center">

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:drawableEnd="@drawable/ic_money_outl"
            android:drawablePadding="@dimen/margin16"
            android:gravity="center"
            android:text="Total Pinjaman"
            android:textColor="@color/white" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="start|center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_currency_dep"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="@dimen/margin8"
            android:gravity="center|clip_vertical"
            android:text="IDR"
            android:textColor="@color/white"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/TEXTVIEW"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="@dimen/margin8"
            android:gravity="center_vertical"
            android:text="100,000,000"
            android:textColor="@color/white"
            android:textSize="18sp" />

    </LinearLayout>
</LinearLayout>


    <GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="@dimen/margin16"
    android:alignmentMode="alignMargins"
    android:columnCount="2"
    android:columnOrderPreserved="false"
    android:padding="@dimen/margin8"
    android:rowCount="2"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout3">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:layout_marginStart="@dimen/margin8"
        android:layout_marginEnd="@dimen/margin8"
        android:layout_marginBottom="@dimen/margin8"
        app:cardCornerRadius="@dimen/margin8"
        app:cardElevation="@dimen/margin8">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingStart="@dimen/margin8"
            android:paddingTop="@dimen/margin16"
            android:paddingEnd="@dimen/margin8"
            android:paddingBottom="@dimen/margin8">

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Pokok"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/textView12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin24"
                android:text="IDR"
                android:textColor="@color/colorPrimaryLight"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/tv_loan_det_prim"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin8"
                android:layout_marginTop="@dimen/margin8"
                android:text="100,000,000"
                android:textColor="@color/colorPrimaryLight"
                android:textSize="18sp" />

        </LinearLayout>

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

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:layout_marginStart="@dimen/margin8"
        android:layout_marginEnd="@dimen/margin8"
        android:layout_marginBottom="@dimen/margin8"
        app:cardCornerRadius="@dimen/margin8"
        app:cardElevation="@dimen/margin8">

        <LinearLayout
            android:id="@+id/rel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingStart="@dimen/margin8"
            android:paddingTop="@dimen/margin16"
            android:paddingEnd="@dimen/margin8"
            android:paddingBottom="@dimen/margin8">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Angsuran"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/textView15"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/margin8"
                    android:layout_marginEnd="@dimen/margin8"
                    android:text="TERTUNGGAK"
                    android:textColor="@color/colorAccent"
                    android:textSize="10sp" />

            </LinearLayout>

            <TextView
                android:id="@+id/textView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin24"
                android:text="IDR"
                android:textColor="@color/colorPrimaryLight"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/textView17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin8"
                android:layout_marginTop="@dimen/margin8"
                android:text="500,000"
                android:textColor="@color/colorAccent"
                android:textSize="18sp" />

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="36dp"
                android:layout_marginStart="@dimen/margin8"
                android:layout_marginTop="@dimen/margin8"
                android:layout_marginEnd="@dimen/margin8"
                android:background="@drawable/bg_green_primary"
                android:text="BAYARAN"
                android:textColor="@color/white" />
        </LinearLayout>

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

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:layout_marginStart="@dimen/margin8"
        android:layout_marginEnd="@dimen/margin8"
        android:layout_marginBottom="@dimen/margin8"
        app:cardCornerRadius="@dimen/margin8"
        app:cardElevation="@dimen/margin8">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingStart="@dimen/margin8"
            android:paddingTop="@dimen/margin16"
            android:paddingEnd="@dimen/margin8"
            android:paddingBottom="@dimen/margin8">

            <TextView
                android:id="@+id/textView18"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Sisa Plafon"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/textView19"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin24"
                android:text="IDR"
                android:textColor="@color/colorPrimaryLight"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/textView20"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin8"
                android:layout_marginTop="@dimen/margin8"
                android:text="9,500,000"
                android:textColor="@color/colorPrimaryLight"
                android:textSize="18sp" />

        </LinearLayout>

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

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:layout_marginStart="@dimen/margin8"
        android:layout_marginEnd="@dimen/margin8"
        android:layout_marginBottom="@dimen/margin8"
        app:cardCornerRadius="@dimen/margin8"
        app:cardElevation="@dimen/margin8">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_rowWeight="1"
            android:orientation="vertical"
            android:paddingStart="@dimen/margin8"
            android:paddingTop="@dimen/margin16"
            android:paddingEnd="@dimen/margin8"
            android:paddingBottom="@dimen/margin8">

            <TextView
                android:id="@+id/textView21"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Tenor/Bunga Bulanan"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/textView22"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin8"
                android:layout_marginTop="@dimen/margin24"
                android:text="12 / 0.50%"
                android:textColor="@color/colorPrimaryLight"
                android:textSize="18sp" />
        </LinearLayout>

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

</GridLayout>

<LinearLayout
    android:id="@+id/linearLayout4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="16dp"
    android:background="@drawable/bg_green_dark"
    android:orientation="horizontal"
    android:padding="@dimen/margin16"
    android:weightSum="4"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent">

    <TextView
        android:id="@+id/textView9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableTop="@drawable/ic_list_inactive"
        android:gravity="center"
        android:text="Senarai Simpanan"
        android:textColor="@color/grey_inactive" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:drawableTop="@drawable/ic_apps"
        android:focusable="true"
        android:gravity="center"
        android:text="Detil"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawableTop="@drawable/ic_history_inactive"
        android:gravity="center"
        android:text="Transaksi"
        android:textColor="@color/grey_inactive" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:drawableTop="@drawable/ic_pen_inactive"
        android:gravity="center"
        android:text="Aplikasi Baru"
        android:textColor="@color/grey_inactive" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>

应该看起来像这样(从Android Studio预览中截取的屏幕截图

grid layout preview

但是实际设备的输出未显示Grid布局。 我有想念的东西吗?

#注意:剩下的布局显示没问题,只有GridLayout。

2 个答案:

答案 0 :(得分:0)

请设置您未定义的GridLayout的高度和宽度,请按照下面给出的代码原样复制并粘贴代码。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <GridLayout
        android:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:alignmentMode="alignMargins"
        android:columnCount="2"
        android:columnOrderPreserved="false"
        android:rowCount="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints">
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
           >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
             >
                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Pokok"
                    android:textColor="@color/black" />
                <TextView
                    android:id="@+id/textView12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="IDR"
                    android:textSize="12sp" />
                <TextView
                    android:id="@+id/tv_loan_det_prim"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="100,000,000"
                    android:textSize="18sp" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
          >
            <LinearLayout
                android:id="@+id/rel"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
              >
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <TextView
                        android:id="@+id/textView14"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Angsuran"
                        android:textColor="@color/black" />
                    <TextView
                        android:id="@+id/textView15"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="TERTUNGGAK"
                        android:textColor="@color/colorAccent"
                        android:textSize="10sp" />
                </LinearLayout>
                <TextView
                    android:id="@+id/textView16"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="IDR"
                    android:textSize="12sp" />
                <TextView
                    android:id="@+id/textView17"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="500,000"
                    android:textColor="@color/colorAccent"
                    android:textSize="18sp" />
                <Button
                    android:id="@+id/button"
                    android:layout_width="match_parent"
                    android:layout_height="36dp"
                    android:text="BAYARAN"
                    android:textColor="@color/white" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"

          >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
               >
                <TextView
                    android:id="@+id/textView18"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Sisa Plafon"
                    android:textColor="@color/black" />
                <TextView
                    android:id="@+id/textView19"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="IDR"
                    android:textSize="12sp" />
                <TextView
                    android:id="@+id/textView20"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="9,500,000"

                    android:textSize="18sp" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
         >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_rowWeight="1"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/textView21"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Tenor/Bunga Bulanan"
                    android:textColor="@color/black" />
                <TextView
                    android:id="@+id/textView22"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="12 / 0.50%"
                    android:textSize="18sp" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </GridLayout>
</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

app.get('/addtodo' , (req, res) => {
    let sql = 'INSERT INTO todo SET ?'
    let post = {
        title: req.body.title,
        body : req.body.body,
        date: req.body.date,
        importance: req.body.importance
    }
    db.query(sql, post, (err, res) => {
        if(err) throw err;
        console.log('success');
        console.log(res);
    });
});

see the below image its shwowing

  implementation 'com.android.support:support-v4:28.0.0'