RecyclerView和CardView的布局问题

时间:2017-03-31 09:35:41

标签: android android-layout android-recyclerview android-cardview

enter image description here

我试图使用CardView和RecyclerView显示一些图像和文本。但我有一些关于layout的问题。我将所有“layout_height”属性设置为“wrap_content”,但ImageView的高度仍然不是我的预期。

MainActivity.java:

package com.example.android.myapplication;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private List<Theme> mThemeList;
    private RecyclerView mList;

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

        mThemeList = getThemes();
        mList = (RecyclerView) findViewById(R.id.theme_list);
        mList.setLayoutManager(new GridLayoutManager(this, 2));
        mList.setAdapter(new ThemeAdapter());
    }

    private class ThemeAdapter extends RecyclerView.Adapter<ThemeAdapter.ViewHolder> {

        @Override
        public ThemeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.theme_item, null, false);

            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            Theme theme = mThemeList.get(position);
            holder.name.setText(theme.getName());
            holder.preview.setImageDrawable(theme.getPreview());
        }

        @Override
        public int getItemCount() {
            return mThemeList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            public TextView name;
            public ImageView preview;

            public ViewHolder(View itemView) {
                super(itemView);

                name = (TextView) itemView.findViewById(R.id.theme_name);
                preview = (ImageView) itemView.findViewById(R.id.theme_preview);
            }
        }
    }

    private List<Theme> getThemes() {
        List<Theme> themes = null;
        Theme theme = null;
        XmlResourceParser parser = getResources().getXml(R.xml.themes);
        try {
            while (parser.getEventType() != XmlResourceParser.END_DOCUMENT) {
                switch (parser.getEventType()) {
                    case XmlResourceParser.START_DOCUMENT:
                        themes = new ArrayList<>(3);
                        break;
                    case XmlResourceParser.START_TAG:
                        String tag = parser.getName();
                        if (Theme.TAG_THEME.equals(tag)) {
                            theme = new Theme();
                        } else if (Theme.TAG_ID.equals(tag)) {
                            parser.next();
                            theme.setId(Integer.parseInt(parser.getText()));
                        } else if (Theme.TAG_NAME.equals(tag)) {
                            parser.next();
                            theme.setName(parser.getText());
                        } else if (Theme.TAG_PREFIX.equals(tag)) {
                            parser.next();
                            theme.setPrefix(parser.getText());
                        } else if (Theme.TAG_PREVIEW.equals(tag)) {
                            parser.next();
                            theme.setPreview(loadPreviewDrawable(parser.getText()));
                        }
                        break;
                    case XmlResourceParser.END_TAG:
                        if (Theme.TAG_THEME.equals(parser.getName())) {
                            themes.add(theme);
                            theme = null;
                        }
                        break;
                    default:
                        break;
                }
                parser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return themes;
    }

    private Drawable loadPreviewDrawable(String resName) {
        Resources resources = getResources();
        int resId = resources.getIdentifier(resName, "drawable", getPackageName());
        Drawable drawable = resources.getDrawable(resId, null);
        return drawable;
    }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
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"
    tools:context="com.example.android.myapplication.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/theme_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

theme_item.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardBackgroundColor="#DDDDDD"
    app:cardCornerRadius="5dp"
    app:cardElevation="10dp"
    app:cardUseCompatPadding="true"
    app:contentPadding="10dp">

    <ImageView
        android:id="@+id/theme_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top" />

    <TextView
        android:id="@+id/theme_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:textSize="18sp" />

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

2 个答案:

答案 0 :(得分:0)

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" <!--You need to change this-->
    app:cardBackgroundColor="#DDDDDD"
    app:cardCornerRadius="5dp"
    app:cardElevation="10dp"
    app:cardUseCompatPadding="true"
    app:contentPadding="10dp">

    <ImageView
        android:id="@+id/theme_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top" />

    <TextView
        android:id="@+id/theme_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:textSize="18sp" />

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

答案 1 :(得分:0)

theme_item.xml 中的android:scaleType="centerCrop"中设置属性ImageView