[英]Centralizing RecyclerView with LinearLayoutManager
我正在尝试将RecyclerView
列表集中在屏幕中。 它将是一个列表,因此我将使用LinearLayoutManager
(LLM)。 但是,使用LLM不会将项目集中,并且在横向模式下看起来像这样:
XML代码如下所示:
<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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/recipe_card"
android:layout_gravity="center"
android:layout_width="400dp"
android:layout_height="186dp"
style="@style/CardViewStyle">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/recipe_image"
android:layout_width="120dp"
android:layout_height="163dp"
app:srcCompat="@mipmap/ic_pan"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_editor_absoluteY="3dp" />
<TextView
android:id="@+id/recipe_name"
android:layout_width="250dp"
android:layout_height="119dp"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/recipe_image"
android:layout_marginLeft="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/recipe_servings"
android:layout_width="164dp"
android:layout_height="32dp"
android:text="TextView"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/recipe_name"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintLeft_toRightOf="@+id/recipe_image"
android:layout_marginLeft="94dp"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
在Android Studio的“预览”上,它看起来不错,但在应用程序上,它看起来并不理想。 我设法通过仅更改一列的GridLayoutManager
的LLM来解决它。 但是看起来很丑。
有人知道发生了什么吗? 谢谢。
尝试将主布局的内容重心设置为居中。 有两种方法可以做到这一点:
希望它能工作,如果不能,那么只给我发送您在布局中使用的图像,我将尝试使用其他技术来修复它,并向您发送更新的代码。
用相对布局替换约束布局
然后,使图像居中,使用此
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="100dp"
android:clickable="true"
android:scaleType="centerCrop" />
然后还要使每个文本成为中心
android:textAlignment="center"
如果您这样写,请检查您的public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, null));
}
这将使您的FrameLayout禁用,而CardView将成为ViewHolder的itemView,因此,您应该进行如下更改: @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); }
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new ViewHolder(mLayoutInflater.inflate(R.layout.your_item_layout, parent, false)); }
希望帮助你。
如果我是你,我将获得显示尺寸的宽度,并相应地为ViewHolder
设置一些边距。
这是我的应用程序类中的代码:
public class MyApp extends Application {
public static Point displaySize = new Point();
public static float density = 1;
public static Context appContext = null;
public static int height;
@Override
public void onCreate() {
super.onCreate();
ActiveAndroid.initialize(this);
appContext = getApplicationContext();
checkDisplaySize();
density = appContext.getResources().getDisplayMetrics().density;
}
public static void checkDisplaySize() {
try {
WindowManager manager = (WindowManager)
appContext.getSystemService(Context.WINDOW_SERVICE);
if (manager != null) {
Display display = manager.getDefaultDisplay();
if (display != null) {
display.getSize(displaySize);
}
height = (int) (displaySize.x / 1.777f); //Aspect Ratio 16:9
}
} catch (Exception e) {
}
}
}
并在适配器中获得“持有者”视图的所有这些之后,只需执行以下操作:
int marginWidth = (MyApp.desplaySize.x - itemView.getMeasuredWidth())/2;
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(marginWidth, 0, marginWidth, 0);
itemView.setLayoutParams(params);
我不太确定这个确切的代码是否可以解决您的问题,但是您可以轻松地对此进行更改。
在您的FrameLayout中编写此代码
android:layout_gravity="center"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.