gpt4 book ai didi

java - Android GridLayout 中的不同高度

转载 作者:行者123 更新时间:2023-11-30 10:01:39 29 4
gpt4 key购买 nike

我正在开发一款外观类似于 Google Keep 的笔记应用。但是,我无法弄清楚如何根据文本长度动态更改便条卡的高度。这是我的应用程序的屏幕截图:

app screenshot

这是卡片的布局文件:

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 的布局:

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"
/>

fragment 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);
}
}

最佳答案

如何根据文本长度动态更改记事卡的高度 - 您只需将布局高度设置为 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>

它看起来像这样:

enter image description here

蓝线代表您的 2 个 View 之间的分隔。

如果您想使用 RecyclerView 实现此目的,请查看 this post , Staggered RecyclerViewHeterogeneous GridLayout

关于java - Android GridLayout 中的不同高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57339772/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com