【问题标题】:How to align first gallery item to the left? [closed]如何将第一个画廊项目向左对齐? [关闭]
【发布时间】:2013-02-26 08:49:19
【问题描述】:

默认情况下,图库是居中对齐的。我想要的行为是左对齐父布局中的第一项而不是居中。怎么做? 我也浏览了链接:https://stackoverflow.com/questions/4341158/android-align-first-item-in-gallery-to-the-left

但这不适用于 2.3.x 设备。 我怎样才能实现它?

【问题讨论】:

  • 添加的链接中建议的答案不适用于 HTC 2.3.3 设备以及 Kindle Fire。

标签: android android-gallery


【解决方案1】:

private void alignGalleryToLeft(画廊画廊) {

    WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

    int galleryWidth = manager.getDefaultDisplay().getWidth();

    // We are taking the item widths and spacing from a dimension resource
    // because:
    // 1. No way to get spacing at runtime (no accessor in the Gallery
    // class)
    // 2. There might not yet be any item view created when we are calling
    // this
    // function
    int itemWidth = getsize(105);
    int spacing = getsize(10);

    // The offset is how much we will pull the gallery to the left in order
    // to simulate left alignment of the first item
    final int offset;
    if (galleryWidth <= itemWidth) {
        offset = galleryWidth / 2 - itemWidth / 2 - spacing;
    } else {
        offset = galleryWidth - itemWidth - 2 * spacing;
    }

    // Now update the layout parameters of the gallery in order to set the
    // left margin
    MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
    mlp.setMargins(-offset, mlp.topMargin, mlp.rightMargin,
            mlp.bottomMargin);
}

getsize() 是一个方法:

public int getsize(int sizeInDip) {
    DisplayMetrics metrics = new DisplayMetrics();
    metrics = getResources().getDisplayMetrics();
    return (int) ((sizeInDip * metrics.density) + 0.5);
}

将您在 DPI 中的值传递给此方法。

希望对你有用。

【讨论】:

    【解决方案2】:

    提供的链接中的答案:https://stackoverflow.com/questions/4341158/android-align-first-item-in-gallery-to-the-left 工作得很好。问题是我在FrameLayout中包装了Gallery,因此当我将其更改为RelativeLayout时它无法正常工作,它工作正常。

    【讨论】:

    • 死链接 404 错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2022-12-18
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多