在 Android 中为 recyclerview 之外的元素获取空白
Getting white spaces for elements outside the recyclerview in Andorid
我有以下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="@style/Theme.MaterialComponents">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/weekDropdown"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:hint="Select Week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:startIconDrawable="@drawable/ic_round_calendar_today_24">
<AutoCompleteTextView
android:id="@+id/weekSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:layout_weight="1"
android:padding="14dp"
android:textSize="16sp"/>
</com.google.android.material.textfield.TextInputLayout>
<RelativeLayout
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"
android:orientation="vertical"
tools:context=".PickemPicksheetActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<EditText
android:id="@+id/tp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/lbl_tp"
android:inputType="number"
android:layout_gravity="center"
android:textAlignment="center"
android:minHeight="48dp"
android:layout_marginBottom="10dp"
android:maxLength="3"
android:textColor="@color/black" />
</LinearLayout>
在布局中我有以下元素:
- TextInputLayout/AutoCompleteTextView 用作下拉菜单
- RecyclerView
- 编辑文本
RecyclerView 链接到以下布局,创建项目列表:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
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:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_centerHorizontal="true"
app:srcCompat="@drawable/gamefield_background" />
<ImageView
android:id="@+id/awayTeam"
android:layout_width="160dp"
android:layout_height="150dp"
android:layout_gravity="top|left" />
<ImageView
android:id="@+id/homeTeam"
android:layout_width="160dp"
android:layout_height="150dp"
android:layout_gravity="top|right"
android:scaleX="-1" />
</androidx.cardview.widget.CardView>
</FrameLayout>
</LinearLayout>
如上所述,这将创建由适配器膨胀的项目列表。
这是上面的输出:
如您所见,在视图的开头应该是下拉菜单的位置有一个白色的大 space!?
只有当我点击它时,因为我知道它在那里才会显示以下内容:
同样的事情发生在视图底部的 EditText 那里有一个大的白色 space 直到我点击那里并输入文本才会出现一些东西!?
我认为这与这两个元素有关:下拉列表和编辑文本位于适配器创建的回收视图的外部...我唯一的问题是我不明白如何修复它?
如果我将这两个元素引入 recylerview,那么它将与游戏条目一起迭代。
有没有办法将元素传递给适配器class?
更新:
为显示白色的视图底部添加图像 space 那里应该是一个 EditText 但不显示,除非我点击那里。
另请注意,当我向上滚动时,向下滚动到列表底部会导致下拉菜单再次变为 hide
,因为 recyclerview 可能会在滚动期间回收视图,所以我必须单击白色space 再次显示它的元素。
这是我的 sheetActivity 调用 recyclerview 的代码:
adapter = new PickemSheetAdapter(sheetActivity.this, gameList, pickList, totalPoints);
recyclerView.setAdapter(adapter);
adapterView如下:
class GameViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView awayTeamImageView, homeTeamImageView;
public GameViewHolder(@NonNull View itemView) {
super(itemView);
awayTeamImageView = itemView.findViewById(R.id.awayTeam);
homeTeamImageView = itemView.findViewById(R.id.homeTeam);
}
public class PickemSheetAdapter extends RecyclerView.Adapter<PickemSheetAdapter.GameViewHolder> {
@Override
public void onBindViewHolder(PickemSheetAdapter.GameViewHolder holder, int position) {
final Games game = gameList.get(position);
holder.awayTeamImageView.setBackgroundResource(awayResourceId);
holder.homeTeamImageView.setBackgroundResource(awayResourceId);
}
}
首先,我建议您找到更多关于 android 布局的资源。你的布局好混乱好复杂。
关于您的问题:您将顶部(自动完成文本)的背景设置为 @null
,因此在您单击之前不会看到它。底部是因为您将布局的 width
设置为包装内容,而里面什么也没有,所以...空白。
再次,我想强调我告诉你的第一件事。
您的问题的主要原因在第 5 行,其中
android:theme="@style/Theme.MaterialComponents"
如果你删除它,一切都会好起来的。
我认为在 Theme.MaterialComponents
中,每种颜色都设置为白色,因此您看不到具有白色背景的元素。如果你坚持使用 Theme.MaterialComponents
那么你必须将你使用的每个元素的颜色更改为另一种颜色。
也没有理由将你的 RecyclerView
放在 RelativeLayout
里面,你应该把它移到外面并删除 RelativeLayout
我有以下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="@style/Theme.MaterialComponents">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/weekDropdown"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:hint="Select Week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:startIconDrawable="@drawable/ic_round_calendar_today_24">
<AutoCompleteTextView
android:id="@+id/weekSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:layout_weight="1"
android:padding="14dp"
android:textSize="16sp"/>
</com.google.android.material.textfield.TextInputLayout>
<RelativeLayout
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"
android:orientation="vertical"
tools:context=".PickemPicksheetActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<EditText
android:id="@+id/tp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/lbl_tp"
android:inputType="number"
android:layout_gravity="center"
android:textAlignment="center"
android:minHeight="48dp"
android:layout_marginBottom="10dp"
android:maxLength="3"
android:textColor="@color/black" />
</LinearLayout>
在布局中我有以下元素:
- TextInputLayout/AutoCompleteTextView 用作下拉菜单
- RecyclerView
- 编辑文本
RecyclerView 链接到以下布局,创建项目列表:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
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:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_centerHorizontal="true"
app:srcCompat="@drawable/gamefield_background" />
<ImageView
android:id="@+id/awayTeam"
android:layout_width="160dp"
android:layout_height="150dp"
android:layout_gravity="top|left" />
<ImageView
android:id="@+id/homeTeam"
android:layout_width="160dp"
android:layout_height="150dp"
android:layout_gravity="top|right"
android:scaleX="-1" />
</androidx.cardview.widget.CardView>
</FrameLayout>
</LinearLayout>
如上所述,这将创建由适配器膨胀的项目列表。
这是上面的输出:
如您所见,在视图的开头应该是下拉菜单的位置有一个白色的大 space!?
只有当我点击它时,因为我知道它在那里才会显示以下内容:
同样的事情发生在视图底部的 EditText 那里有一个大的白色 space 直到我点击那里并输入文本才会出现一些东西!?
我认为这与这两个元素有关:下拉列表和编辑文本位于适配器创建的回收视图的外部...我唯一的问题是我不明白如何修复它?
如果我将这两个元素引入 recylerview,那么它将与游戏条目一起迭代。
有没有办法将元素传递给适配器class?
更新:
为显示白色的视图底部添加图像 space 那里应该是一个 EditText 但不显示,除非我点击那里。
另请注意,当我向上滚动时,向下滚动到列表底部会导致下拉菜单再次变为 hide
,因为 recyclerview 可能会在滚动期间回收视图,所以我必须单击白色space 再次显示它的元素。
这是我的 sheetActivity 调用 recyclerview 的代码:
adapter = new PickemSheetAdapter(sheetActivity.this, gameList, pickList, totalPoints);
recyclerView.setAdapter(adapter);
adapterView如下:
class GameViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView awayTeamImageView, homeTeamImageView;
public GameViewHolder(@NonNull View itemView) {
super(itemView);
awayTeamImageView = itemView.findViewById(R.id.awayTeam);
homeTeamImageView = itemView.findViewById(R.id.homeTeam);
}
public class PickemSheetAdapter extends RecyclerView.Adapter<PickemSheetAdapter.GameViewHolder> {
@Override
public void onBindViewHolder(PickemSheetAdapter.GameViewHolder holder, int position) {
final Games game = gameList.get(position);
holder.awayTeamImageView.setBackgroundResource(awayResourceId);
holder.homeTeamImageView.setBackgroundResource(awayResourceId);
}
}
首先,我建议您找到更多关于 android 布局的资源。你的布局好混乱好复杂。
关于您的问题:您将顶部(自动完成文本)的背景设置为 @null
,因此在您单击之前不会看到它。底部是因为您将布局的 width
设置为包装内容,而里面什么也没有,所以...空白。
再次,我想强调我告诉你的第一件事。
您的问题的主要原因在第 5 行,其中
android:theme="@style/Theme.MaterialComponents"
如果你删除它,一切都会好起来的。
我认为在 Theme.MaterialComponents
中,每种颜色都设置为白色,因此您看不到具有白色背景的元素。如果你坚持使用 Theme.MaterialComponents
那么你必须将你使用的每个元素的颜色更改为另一种颜色。
也没有理由将你的 RecyclerView
放在 RelativeLayout
里面,你应该把它移到外面并删除 RelativeLayout