android 4.4中与RecyclerView的对齐问题
我在我的应用程序中实现了一个,它正在工作。但我在Recyclerview中面临列表项对齐的问题 实际上,我的列表项有一个图像视图和一个文本视图。我需要在每个列表项之间留出一些空间。像这样 有了这个相对的布局,我能够得到我预期的结果。但是对于项目之间的间距,我使用了 这是我的列表_view.xmlandroid 4.4中与RecyclerView的对齐问题,android,xml,layout,android-recyclerview,Android,Xml,Layout,Android Recyclerview,我在我的应用程序中实现了一个,它正在工作。但我在Recyclerview中面临列表项对齐的问题 实际上,我的列表项有一个图像视图和一个文本视图。我需要在每个列表项之间留出一些空间。像这样 有了这个相对的布局,我能够得到我预期的结果。但是对于项目之间的间距,我使用了 这是我的列表_view.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.androi
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#65FF0000"
android:padding="10dip" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:layout_toRightOf="@+id/item_icon"
android:gravity="left"
android:textSize="24sp" />
</RelativeLayout>
我没有使用,而是用下面的方法尝试了,但不起作用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#90FF0000"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/item_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.6"
android:text="TextView"
android:textColor="@android:color/darker_gray"
android:textSize="24dp" />
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="5dp"
android:text=""
android:textColor="#90FFFFFF" />
</LinearLayout>
但我看到的是下面这样的景象
我到底在哪里犯错。。提前感谢您需要使列表项填充其父项的整个宽度 详情如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90FF0000"
android:orientation="horizontal">
<ImageView
android:layout_weight="1"
android:id="@+id/item_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_weight="1"
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView"
android:textColor="@android:color/darker_gray"
android:textSize="24dp" />
<TextView
android:layout_weight="1"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="5dp"
android:text=""
android:textColor="#90FFFFFF" />
</LinearLayout>
</LinearLayout>
您需要使列表项填充其父项的整个宽度 详情如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90FF0000"
android:orientation="horizontal">
<ImageView
android:layout_weight="1"
android:id="@+id/item_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_weight="1"
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView"
android:textColor="@android:color/darker_gray"
android:textSize="24dp" />
<TextView
android:layout_weight="1"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="5dp"
android:text=""
android:textColor="#90FFFFFF" />
</LinearLayout>
</LinearLayout>
我从您的问题中了解到,您希望您的
回收视图的项目如下所示:
下面是一个简单的布局,它将创建此外观(使用RelativeLayout
):
如果您想在两个RecyclerView
项目之间留出空间,无需再添加另一个TextView
,只需在list\u item.xml中的主容器中添加padding
,我从您的问题中了解到,您希望您的RecyclerView
项目如下所示:
下面是一个简单的布局,它将创建此外观(使用RelativeLayout
):
如果你想在两个RecyclerView
项目之间留出空间,无需再添加另一个TextView
只需在list\u item.xml中的主容器中添加padding
。最后,我找到了问题的解决方案
在我之前的RecyclerView适配器中,我使用了以下类型的方法来创建列表项
public MyAdapter.ViewHolder onCreateViewHolder( ViewGroup parent,
int viewType )
{
// create a new view
itemLayoutView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.list_row_serch,
null );
// create ViewHolder
ViewHolder viewHolder = new ViewHolder( itemLayoutView );
return viewHolder;
}
如下图所示进行更改后,工作正常。即使它是一个相关/线性布局
public MyAdapter.ViewHolder onCreateViewHolder( ViewGroup parent,
int viewType )
{
itemLayoutView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.list_row_serch,
parent,
false );
// create ViewHolder
ViewHolder viewHolder = new ViewHolder( itemLayoutView );
return viewHolder;
}
最后,我找到了问题的答案
在我之前的RecyclerView适配器中,我使用了以下类型的方法来创建列表项
public MyAdapter.ViewHolder onCreateViewHolder( ViewGroup parent,
int viewType )
{
// create a new view
itemLayoutView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.list_row_serch,
null );
// create ViewHolder
ViewHolder viewHolder = new ViewHolder( itemLayoutView );
return viewHolder;
}
如下图所示进行更改后,工作正常。即使它是一个相关/线性布局
public MyAdapter.ViewHolder onCreateViewHolder( ViewGroup parent,
int viewType )
{
itemLayoutView = LayoutInflater.from( parent.getContext() ).inflate( R.layout.list_row_serch,
parent,
false );
// create ViewHolder
ViewHolder viewHolder = new ViewHolder( itemLayoutView );
return viewHolder;
}
你的预期结果是什么?我更新了我的问题,并在@MarcusSo发布了预期结果。你想要一个带有图像/图标和两个文本视图的列表项,一个在另一个下面,与图标右侧对齐?我只需要一个文本视图。我给出的第二个文本视图是为了在列表项之间留出空间@Android developer您的预期结果是什么?我更新了我的问题并发布了预期结果@MarcusSo您想要一个带有图像/图标的列表项,两个文本视图一个在另一个列表项下,与图标右侧对齐?我只需要一个文本视图。我给出的第二个文本视图是为了在@Android developer列表项之间留出空间,请查看我的更新答案。您必须调整重量,使其符合您的需要@在发布问题之前,我尝试了所有的组合@Marcus。不适用于我的最新答案。您必须调整重量,使其符合您的需要@在发布问题之前,我尝试了所有的组合@Marcus。不适用于meSee使用LinearLayout
编辑的版本答案。相对布局工作正常。但是线性布局不起作用。。谢谢你的帮助线性布局有什么问题吗?我已经测试了这两个版本,并且输出是相同的。所以您想在项目之间添加分隔符吗?如果是,您应该使用您在问题中所说的itemdcorator
。这是正确的方法。让我们来看看使用LinearLayout
编辑的版本答案。相对布局工作正常。但是线性布局不起作用。。谢谢你的帮助线性布局有什么问题吗?我已经测试了这两个版本,并且输出是相同的。所以您想在项目之间添加分隔符吗?如果是,您应该使用您在问题中所说的itemdcorator
。这是正确的方法,让我们来。