Android 以片段为中心垂直循环视图
首先,我对安卓系统有点陌生。 我目前有一个加载片段的活动,在这个片段中,我实例化了一个RecyclerView(具有水平方向) 以下是我当前的布局: 我的主要意见是:Android 以片段为中心垂直循环视图,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,首先,我对安卓系统有点陌生。 我目前有一个加载片段的活动,在这个片段中,我实例化了一个RecyclerView(具有水平方向) 以下是我当前的布局: 我的主要意见是: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" a
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:orientation="vertical">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green">
</FrameLayout>
</LinearLayout>
我的片段:
<LinearLayout 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:gravity="center_vertical"
android:orientation="vertical"
android:background="@color/yellow">
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_atelier_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:background="#FFE5E5E5"
android:scrollbarStyle="insideOverlay"
/>
</LinearLayout>
我的RecyclerView适配器的我的项目布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@color/gray">
<ImageView
android:id="@+id/item_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_gravity="center"
android:contentDescription="icon"/>
</LinearLayout>
我尝试了几个小时使我的RecyclerView垂直居中,但没有成功(通过使用重力、layout_centerVertical等…)。
也许我遗漏了一些关于包装内容/匹配父级或线性/相对规格的内容
提前感谢并祝您有一个愉快的一天:)这应该可以做到:
<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:background="@color/yellow">
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_atelier_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#FFE5E5E5"
android:scrollbarStyle="insideOverlay"
/>
</RelativeLayout>
这是我将项目垂直居中所做的:
mRecyclerViewView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
try {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
params.height = parent.getHeight();
view.setLayoutParams(params);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
然后在我的项目布局中,我将子视图的宽度/高度设置为
fill\u parent
,将重力设置为center
android:layout\u centerInParent=“true”
仅在相对布局中有效,而在线性布局中无效。而且它足够垂直和水平居中,不需要额外的android:layout\u centerVertical=“true”
我同意。当我把它改成RelativeLayout时,我错过了它。感谢您的回复,我尝试了您的解决方案,但效果相同:我的recyclerView粘贴到我的显示器顶部。我的应用程序使用了横向模式。你能上传一个屏幕截图吗?最后,我通过保持线性布局和在回收器/父布局上使用重心解决了我的问题。此外,我删除了item_layout.Erf中的重力参数,我在平板电脑上进行了尝试,回收器现在位于左侧:'|