如何使cardview响应?

时间:2018-04-18 11:54:58

标签: java android android-layout grid-layout android-cardview

This is how my cardview look like...But when I change the rotation of android this heppens look the second picture.....[![This heppened when I change the rotation   ] 2] 2

这是NamazActivity.java 在java类中,我只需添加cardview click监听器并打开Toast消息。每个单击侦听器都显示带有简单文本的iteam ID

package com.example.shakeelmughal.assanislam;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;

public class NamazActivity extends AppCompatActivity {

    GridLayout gridLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_namaz);

        //back button
        if(getSupportActionBar()!= null)
        {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        gridLayout = findViewById(R.id.gridlayout);
        setSingleClick(gridLayout);
    }

    //function for going back to previous activity
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home)
            finish();
        return super.onOptionsItemSelected(item);
    }

    private void setSingleClick(GridLayout gridLayout) {
        for (int i = 0; i < gridLayout.getChildCount(); i++) {
            CardView cardView = (CardView) gridLayout.getChildAt(i);
            final int s = i;
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(NamazActivity.this, "Item " + s + " Clicked", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}

这是nazamactivity.xml 在XML类中,我使用gridlayout,在其中我使用带有文本视图和图像视图的cardview。我已经制作了3张卡片......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    tools:context="com.example.shakeelmughal.assanislam.NamazActivity"
    android:orientation="vertical"
    android:weightSum="5">

    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <TextView
            android:id="@+id/textGrid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40sp"
            android:textColor="#000"
            android:layout_centerInParent="true"
            android:text="نماز"/>
    </RelativeLayout>

    <GridLayout
        android:id="@+id/gridlayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentTop="true"
        android:layout_weight="4"
        android:alignmentMode="alignMargins"
        android:columnCount="1"
        android:columnOrderPreserved="false"
        android:padding="14dp"
        android:rowCount="3">

        <!-- Row 1 -->
        <!-- Col 1 -->
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_rowWeight="1"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/tahart"
                    android:paddingRight="70dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text= " Ùرض نمازیں"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="25sp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="25sp"
                    android:textColor="#000"
                    android:text="5"
                    android:paddingLeft="10dp"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <!-- Row 2 -->
        <!-- Col 1 -->
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_rowWeight="1"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/namaz"
                    android:paddingRight="70dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="واجب نمازیں"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="25sp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#000"
                    android:text="3"
                    android:textSize="25sp"
                    android:paddingLeft="10dp"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <!-- Row 3 -->
        <!-- Col 1 -->
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_rowWeight="1"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:paddingLeft="10dp"
                    android:paddingRight="80dp"
                    android:src="@drawable/namaz" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ù†ÙÙ„ نمازیں"
                    android:textAlignment="center"
                    android:textColor="#000"
                    android:textSize="25sp"
                    android:paddingRight="20dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4"
                    android:textSize="25sp"
                    android:textColor="#000"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

    </GridLayout>


</LinearLayout>

1 个答案:

答案 0 :(得分:1)

尝试将 ScrollView 设为您的父布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="5">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <TextView
                android:id="@+id/textGrid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="نماز"
                android:textColor="#000"
                android:textSize="40sp" />
        </RelativeLayout>

        <GridLayout
            android:id="@+id/gridlayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_alignParentTop="true"
            android:layout_weight="4"
            android:alignmentMode="alignMargins"
            android:columnCount="1"
            android:columnOrderPreserved="false"
            android:padding="14dp"
            android:rowCount="3">

            <!-- Row 1 -->
            <!-- Col 1 -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_rowWeight="1"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:paddingRight="70dp"
                        android:src="@drawable/ic_message" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=" Ùرض نمازیں"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="25sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="10dp"
                        android:text="5"
                        android:textColor="#000"
                        android:textSize="25sp" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <!-- Row 2 -->
            <!-- Col 1 -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_rowWeight="1"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:paddingRight="70dp"
                        android:src="@drawable/ic_message" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="واجب نمازیں"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="25sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="10dp"
                        android:text="3"
                        android:textColor="#000"
                        android:textSize="25sp" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <!-- Row 3 -->
            <!-- Col 1 -->
            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_rowWeight="1"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:paddingLeft="10dp"
                        android:paddingRight="80dp"
                        android:src="@drawable/ic_message" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingRight="20dp"
                        android:text="Ù†ÙÙ„ نمازیں"
                        android:textAlignment="center"
                        android:textColor="#000"
                        android:textSize="25sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="4"
                        android:textColor="#000"
                        android:textSize="25sp" />
                </LinearLayout>
            </android.support.v7.widget.CardView>

        </GridLayout>


    </LinearLayout>
</ScrollView>