gpt4 book ai didi

android - 将 recyclerview 的最后两项居中?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:50 26 4
gpt4 key购买 nike

如何将具有 3 列的 RecyclerView 的最后两项居中?

包含 5 个项目的示例:

1 item | 2 item | 3 item
4 item | 5 item

图片说明 Image to explain

最佳答案

我遇到了同样的情况,我发现了一个非常有趣的东西:FlowLayout。

我找到了这个库,它真的很容易使用,这里是说明:https://github.com/ApmeM/android-flowlayout .它会自动将元素居中!

例如,在我的例子中,在使用以下命令在 gradle 中添加库之后:compile 'org.apmem.tools:layouts:1.10@aar',我为此更改了 RecyclerView 元素:

<org.apmem.tools.layouts.FlowLayout
android:id="@+id/lytXXXXX"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layoutDirection="ltr" (Left to right)
android:orientation="horizontal" />

更改后,我创建了方法 loadElementsToTheFlowLayout()。在这里:

private void loadElementsToTheFlowLayout() {
// Remove all the FlowLayout elements
flowLayout.removeAllViews();

// For each element, I create its view and I add it to the FlowLayout
for (final Element e : elementsList) {
// Inflate the e view to the flow layout
View viewElement = layoutInflater.inflate(R.layout.elements_item, flowLayout, false);

// Create the element view with its data
FrameLayout elementView = (FrameLayout) viewElement .findViewById(R.id.lytForElement);

TextViewFont txtLine0 = (TextViewFont) viewElement .findViewById(R.id.txtLine0);
txtLine0.setText(e.getLine0());

TextViewFont txtLine1 = (TextViewFont) viewElement .findViewById(R.id.txtLine1);
txtLine1.setText(e.getLine1());

TextViewFont txtLine2 = (TextViewFont) viewElement .findViewById(R.id.txtLine2);
txtLine2.setText(e.getLine2());

TextViewFont txtLine3 = (TextViewFont) viewElement .findViewById(R.id.txtLine3);
txtLine3.setText(e.getLine3());

// Add the view to the FlowLayout
flowLayout.addView(viewElement );
}
}

希望对您有所帮助!

关于android - 将 recyclerview 的最后两项居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41299346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com