首页 > 解决方案 > Android GridLayout 中的不同高度

问题描述

我正在创建一个外观类似于 Google Keep 的笔记应用程序。但是,我不知道如何根据文本长度动态更改笔记卡的高度。这是我的应用程序的屏幕截图:

应用截图

这是卡片的布局文件:

notes_card.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:id="@+id/notes_card"
    android:layout_width="match_parent"
    android:layout_gravity="center"
    app:cardElevation="3dp"
    android:layout_margin="4dp"
    app:cardUseCompatPadding="true"
    android:layout_height="wrap_content"
    card_view:cardMaxElevation="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/note_text1"
            android:minLines="3"
            android:maxLines="15"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

笔记片段的布局:

fragment_notes_view.xml

<androidx.recyclerview.widget.RecyclerView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notes_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    />

片段 onViewCreated 函数:

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        //Initialize the RecyclerView and push it onto the fragment
        RecyclerView recyclerView = view.findViewById(R.id.notes_recyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(NotesView.this.getContext(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        NotesRecyclerAdapter adapter = new NotesRecyclerAdapter(this.getContext());
        recyclerView.setAdapter(adapter);
        super.onViewCreated(view, savedInstanceState);
    }

RecyclerViewAdapter 代码:

package xyz.incepted.thereminder.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import xyz.incepted.thereminder.R;
import xyz.incepted.thereminder.types.QuickNote;

public class NotesRecyclerAdapter extends RecyclerView.Adapter <NoteViewHolder> {
    //Initialize class variables
    private Context context;
    private ArrayList<QuickNote> notes;
    private NotesConnector notesConnector = new NotesConnector();

    /**
     * Creates a RecylerViewAdapter based on the context, also fetches the notes from the NotesConnector
     * @param context context to be based on
     */
    public NotesRecyclerAdapter(Context context){
        this.context = context;
        this.notes = notesConnector.getNotes();
    }

    /**
     * Executed when the RecylerView is created
     * @param parent parent of the view
     * @param viewType type of the view
     * @return
     */
    @NonNull
    @Override
    public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //Inflate the view and return it
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.notes_card, parent, false);
        return new NoteViewHolder(view);
    }

    /**
     * Sets the view properties
     * @param holder holder
     * @param position which text to be used
     */
    @Override
    public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
        holder.text1.setText(notes.get(position).getText1());
    }

    /**
     * Returns the item size of the list
     * @return item size
     */
    @Override
    public int getItemCount() {
        return notes.size();
    }
}

/**
 * Class to hold NoteView
 * //TODO divide it into two, since QuickNote and Note will have different designs
 */
class NoteViewHolder extends RecyclerView.ViewHolder{
    TextView text1;
    NoteViewHolder(View view){
        super(view);
        text1 = view.findViewById(R.id.note_text1);
    }
}

标签: javaandroidandroid-recyclerviewandroid-gridlayout

解决方案


如何根据文本长度动态更改笔记卡的高度- 您可以将布局高度设置为wrap_content 如下所示:

android:layout_height="wrap_content"

您可以像这样将您的项目放在 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"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="short text !!!!!!!!!!!!?!@#!@%$%$#"
        android:layout_marginBottom="8dp"/>


    <TextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="Longggggggggggggggggggggggggggggggggggggggggggggggg  text very long"/>
</LinearLayout>

</ScrollView>

它看起来像这样:

在此处输入图像描述

蓝线表示您的 2 个视图之间的分隔。

如果您想使用 RecyclerView 来实现这一点,请查看这篇文章交错 RecyclerView异构 GridLayout


推荐阅读