android 4.4 中 RecyclerView 的对齐问题
alignment issue with RecyclerView in android 4.4
我正在我的应用程序中实施 RecyclerView,它正在运行。但我在 Recyclerview 中遇到列表项对齐问题。
实际上我的列表项有一个图像视图和一个文本视图。我需要在每个列表项之间添加一些 space。像这样
通过这种相对布局,我可以获得预期的结果。但是对于项目之间的间距,我使用了 RecyclerView.ItemDecoration
这是我的 list_view.xml
<?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>
代替使用 RecyclerView.ItemDecoration 我尝试了以下方法,但它不是 worked.Here 是我的 list_item.xml 代码
<?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>
根据您的问题,我了解到您希望 RecyclerView
的项目看起来像这样:
这是一个简单的布局,可以创建这种外观(使用 RelativeLayout
):
<?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:padding="10dip">
<ImageView
android:id="@+id/user_avatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:layout_toRightOf="@+id/user_avatar"
android:gravity="left"/>
</RelativeLayout>
这里是使用 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="horizontal"
android:padding="10dip">
<ImageView
android:id="@+id/user_avatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_gravity="center_vertical"
android:gravity="left"/>
</LinearLayout>
如果你想在两个 RecyclerView
项目之间 space 不需要添加另一个 TextView
只需将 padding
添加到 list_item.xml
[= 中的主容器20=]
好吧,我终于找到了问题的解决方案。
在我之前的 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;
}
按如下所示更改后,它工作正常。即使它是 Releative/Linear 布局
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,它正在运行。但我在 Recyclerview 中遇到列表项对齐问题。
实际上我的列表项有一个图像视图和一个文本视图。我需要在每个列表项之间添加一些 space。像这样
通过这种相对布局,我可以获得预期的结果。但是对于项目之间的间距,我使用了 RecyclerView.ItemDecoration
这是我的 list_view.xml
<?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>
代替使用 RecyclerView.ItemDecoration 我尝试了以下方法,但它不是 worked.Here 是我的 list_item.xml 代码
<?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>
根据您的问题,我了解到您希望 RecyclerView
的项目看起来像这样:
这是一个简单的布局,可以创建这种外观(使用 RelativeLayout
):
<?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:padding="10dip">
<ImageView
android:id="@+id/user_avatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:layout_toRightOf="@+id/user_avatar"
android:gravity="left"/>
</RelativeLayout>
这里是使用 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="horizontal"
android:padding="10dip">
<ImageView
android:id="@+id/user_avatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_gravity="center_vertical"
android:gravity="left"/>
</LinearLayout>
如果你想在两个 RecyclerView
项目之间 space 不需要添加另一个 TextView
只需将 padding
添加到 list_item.xml
[= 中的主容器20=]
好吧,我终于找到了问题的解决方案。
在我之前的 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;
}
按如下所示更改后,它工作正常。即使它是 Releative/Linear 布局
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;
}