pcatzj
V2EX  ›  Android

android 小白又来求教关于 RecyclerView 的问题啦

  •  
  •   pcatzj · Mar 28, 2016 · 16457 views
    This topic created in 3872 days ago, the information mentioned may be changed or developed.

    一个水平方向的 RecyclerView ,每个 Item 是一个 ImageView ,整体的 width 是 match_parent 的。 现在有三个 item ,总体宽度远小于屏幕宽度。现在怎么让这三个 item 居中,怎么设置。

    8 replies  •  2016-03-29 09:00:13 +08:00
    jedrek
    jedrek
    Mar 28, 2016
    1
    如果你的 item 不会动态增加,这么用 RecyclerView 并不好。 RecyclerView 的设置成 match_parent 也使得 RecyclerView 没啥意义了。偏要这么做也可以的。
    使用 RecyclerView.addItemDecoration(...) 可以上下左右及 item 周边添加空白
    wenmingvs
    wenmingvs
    Mar 28, 2016
    2
    可以在 adapter 内,给这三个 imageview 手动设置固定的宽度,这样的话可以让三个 ImageView 很平均的分布在屏幕上
    pcatzj
    pcatzj
    OP
    Mar 28, 2016
    3
    @jedrek 是要动态添加的,另外说错了, width 是 wrap_content 的,用 Decoration 设置了 item 的间距,但是现在它在水平方向上不居中,在 android studio 上面写的 demo 是居中的,但是拷贝到 eclipse 的项目里就不好使了
    pcatzj
    pcatzj
    OP
    Mar 28, 2016
    4
    @wenmingvs 三个 ImageView 是固定宽高的
    allan1st
    allan1st
    Mar 28, 2016
    5
    加 paddings 然后 clipToPadding false ,参考 TabLayout 的做法。
    pcatzj
    pcatzj
    OP
    Mar 28, 2016
    6
    @allan1st 但是我要让它水平居中啊,而且 item 的数量时不确定的
    EdiTurn
    EdiTurn
    Mar 28, 2016
    7
    RecyclerView 的 layout_width 设为 wrap_content , layout_gravity 设为 center_horizontal 。
    记得用最新的 support library.
    pcatzj
    pcatzj
    OP
    Mar 29, 2016
    8
    @EdiTurn 现在解决了,在 ItemDecorarion 里动态计算了四边边距。估计我的包不是最新的,但是公司的项目,我就不想动了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5124 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:41 · PVG 15:41 · LAX 23:41 · JFK 02:41
    ♥ Do have faith in what you're doing.