选择的图库默认项目位于中心

发布于 2024-10-25 12:41:26 字数 335 浏览 1 评论 0原文

可能的重复:
android图库图像位置问题

我正在我的应用程序中使用图库视图。现在当我运行代码时。图库默认选择的项目为 1,位于中心,左侧为空白。 相反,我不希望左侧有 1 项被选中。 此外,单击任何图库项目不应将该项目置于中心。

我厌倦了在团体中寻找很多东西,但没有找到任何解决方案。这可能吗? 如果是的话怎么会呢?

Possible Duplicate:
android gallery image position problem

I'm using Gallery view within my app. Now when I run the code. Gallery has default selected item is no 1 which is in center and left side is blank.
Instead I want no 1 item should be at left and selected.
Also clicking on the any gallery item should not bring that item in the center.

I tired hunting it lot on the groups but not found any solution. Is this possible or not ?
If yes then how come?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白日梦 2024-11-01 12:41:26

嘿,我在调整左边距时遇到了同样的问题,如下所示:

DisplayMetrics metrics = new DisplayMetrics();
ctx.getWindowManager().getDefaultDisplay().getMetrics(metrics);

Gallery gallery = (Gallery) ctx.findViewById(R.id.gallery);

MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
mlp.setMargins(-(metrics.widthPixels/2), 
               mlp.topMargin, 
               mlp.rightMargin, 
               mlp.bottomMargin
);

它几乎到达左边缘,但有一点松弛(一半图像宽度?),我实际上很喜欢;

如果你

-(metrics.widthPixels/2)

-(metrics.widthPixels/2 + YOUR_IMAGE_WIDTH)

它替换,它会一直到屏幕的左边缘(假设这个画廊显示相同大小的图像,这就是我的情况)

hey, I kinda got around the very same issue with adjusting the left margin like so:

DisplayMetrics metrics = new DisplayMetrics();
ctx.getWindowManager().getDefaultDisplay().getMetrics(metrics);

Gallery gallery = (Gallery) ctx.findViewById(R.id.gallery);

MarginLayoutParams mlp = (MarginLayoutParams) gallery.getLayoutParams();
mlp.setMargins(-(metrics.widthPixels/2), 
               mlp.topMargin, 
               mlp.rightMargin, 
               mlp.bottomMargin
);

which goes almost to the left edge, but there is a bit of slack (half image width?), which I actually liked;

if you replace

-(metrics.widthPixels/2)

with

-(metrics.widthPixels/2 + YOUR_IMAGE_WIDTH)

it goes all the way to left edge of the screen (provided this gallery is displaying images of same size, which was my case)

心是晴朗的。 2024-11-01 12:41:26

Gallery 没有自定义挂钩来支持在没有自定义实现的情况下执行您想要的操作。您应该查看的是 Gallery 小部件的源代码:

Gallery.java

使用它作为自定义小部件的起点,该小部件完全可以满足您的需求。特别要注意 setSelectionToCenterChild() 方法,它是当前 Gallery 如何进行选择的一部分。许多方法都不是私有的,因此您可能能够摆脱子类化和重写方法......但很可能您需要获取此源并创建一个新类。

希望有帮助!

Gallery does not have the customization hooks to support doing what you want without a custom implementation. What you should take a look at is the source code for the Gallery widget:

Gallery.java

Use this as a starting point for a custom widget that does exactly what you want. In particular, pay attention to the method setSelectionToCenterChild() which is part of how the current Gallery does it's selection. Many of the methods are not private, so you may be able to get away with subclassing and overriding methods...but most likely you will need to take this source and create a new class.

Hope that Helps!

趁年轻赶紧闹 2024-11-01 12:41:26

它对我有用:

mlp.setMargins((int) -(metrics.widthPixels/2.5), mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);

it works for me with this :

mlp.setMargins((int) -(metrics.widthPixels/2.5), mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文