简体   繁体   中英

Grid Layout Isn't Showing in Android Fragment

I want to create some 4 grid layout which is have Constraint Layout as a root layout. The file is created to be as a fragment, but I think that won't be a problem, mentioned in title to describe it as detail as possible.

The xml file is looked like this:

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

And it should be looked like this ( screenshot taken from Android Studio preview )

网格布局预览

But the output in the real device the Grid layout is not showing. Is there a thing I miss?

#Note: The rest layout is showed just fine no problem, only the GridLayout.

Please set the height and width of the GridLayout, where you didn't define, please copy and paste the code as it is which is given below it will definitely work.

<?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>
  implementation 'com.android.support:support-v4:28.0.0'

见下图所示

  implementation'com.android.support:design:28.0.0'

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