带正方形的2x2 GridLayout

时间:2018-03-27 12:19:11

标签: android xml android-layout

我想要一个2x2网格布局,使用正方形的CardView填充整个屏幕宽度。

目前,我有这个

enter image description here

CardViews被拉伸到GridLayout的高度。我怎样才能使CardView高度与其宽度相匹配,所以我有正方形?

我当前的XML看起来像这样:

<?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.jonas.trainingslog1.Activity_Settings"
android:orientation="vertical">

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="600dp"
    android:columnCount="2"
    android:rowCount="2"
    android:alignmentMode="alignMargins"
    android:columnOrderPreserved="false"
    android:layout_margin="16dp">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_margin="16dp"/>

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_margin="16dp"/>

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_margin="16dp"
        android:layout_rowWeight="1" />

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_margin="16dp"/>


</GridLayout>

我搜索了很多,但没有真正帮助过,欢迎任何建议!

2 个答案:

答案 0 :(得分:1)

尝试将CardView的宽度和高度设置为某些预期的相等值,例如200dp:

<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="600dp"
    android:columnCount="2"
    android:rowCount="2"
    android:alignmentMode="alignMargins"
    android:columnOrderPreserved="false"
    android:layout_margin="16dp">

    <android.support.v7.widget.CardView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_margin="16dp"/>

    (...)

编辑:

或尝试设置:

<GridLayout
    android:layout_width="wrap_content"

答案 1 :(得分:1)

尝试:

enter image description here

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import android.support.v7.app.AlertDialog;

import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;



public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ListView simpleList;

        final String[] courseList = new String[3];// = {"HR","C Programming", "C++","C#", "Java", "Javascript", "Angular JS", "Data Structure", "Ajax", "Asp.Net", "jQuery", "json", "SQL"};
        int flags[] = {R.drawable.hr,R.drawable.cprogramming, R.drawable.cplus, R.drawable.csharp, R.drawable.java, R.drawable.javascript, R.drawable.angularjs, R.drawable.datastructures, R.drawable.ajax, R.drawable.aspnet, R.drawable.jquery, R.drawable.json, R.drawable.sql};

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // for volley
        String jsonURL = "http://192.168.0.131:10462/WebService1.asmx";
        final String data = "";
        RequestQueue requestQueue;

        requestQueue = Volley.newRequestQueue(this);
        JSONObject jsonRequest = new JSONObject();
        try {
            jsonRequest.put("GetTopic",Request.Method.GET );
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonURL,jsonRequest,
            new Response.Listener<JSONObject>() {

                // Takes the response from the JSON request
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONObject obj = response.getJSONObject("GetTopic");
                        // Retrieves the string labeled "colorName" and "description" from
                        //the response JSON Object
                        //and converts them into javascript objects
                        String color = obj.getString("GetTopic");
                       // Adds strings from object to the "data" string
                        String data = "Topic Name: " + color ;
                                // Adds the data string to the TextView "results"
                       // results.setText(data);
                        courseList[0] = data;
                        courseList[1] = data;
                        courseList[2] = data;

                       // Toast.makeText(getApplicationContext(),data,Toast.LENGTH_LONG).show();
                    }
                    // Try and catch are included to handle any errors due to JSON
                    catch (JSONException e) {
                        // If an error occurs, this prints the error to the log
                        e.printStackTrace();
                    }
                }
            },  new Response.ErrorListener() {
                @Override
                // Handles errors that occur due to Volley
                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley", "Error");
                }
            });

        requestQueue.add(jsonObjectRequest);

        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), courseList, flags);
}