java - 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);
}
}
解决方案
如何根据文本长度动态更改笔记卡的高度- 您可以将布局高度设置为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
推荐阅读
- c# - ASP.Net Core 3.1:控制器方法总是从 HttpPost 调用中接收 NULL 参数
- python - 如何在 matplotlib 中绘制二维结构化网格
- elasticsearch - 按术语分组并获取嵌套数组属性的计数?
- android - Android Q,如何使用正确的文件名使用 MediaStore 保存录音?
- video - 视频标签中 WebP 海报的后备,如何?
- parquet - 是否可以将多个 oracle 数据库表写入一个 parquet 文件?
- while-loop - 使用 LCD Arduino 计算值并执行计算
- pytorch - pytorch-lightning 中的自定义后向/优化步骤
- excel - 当包含公式的单元格的值第一次更新时,如何设置时间戳?
- javascript - Redux Action Creator 导致无限循环