Android Gallery 第一项不显示:parent.getWidth 返回 0
我有一个显示 TextViews 的图库。显示时,第一项不会显示。这是因为parent.getWidth() 在第一次调用时返回0。
那么如何将项目的宽度设置为其父画廊宽度的一小部分呢?
这是代码:
public class GalleryActivity extends Activity {
private Gallery gallery;
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.gallery);
gallery = (Gallery) findViewById (R.id.gallery);
gallery.setAdapter (new Adapter ());
}
private class Adapter extends BaseAdapter {
private String[] items = {"A", "B", "C", "D", "E", "F"};
public int getCount () {
return items.length;
}
public Object getItem (int position) {
return items[position];
}
public long getItemId (int position) {
return position;
}
public View getView (int position, View convertView, ViewGroup parent) {
TextView view = new TextView (GalleryActivity.this);
view.setText (getItem (position).toString ());
view.setBackgroundColor (Color.GRAY);
view.setGravity (Gravity.CENTER);
view.setLayoutParams (new Gallery.LayoutParams (parent.getWidth () / 3,
LayoutParams.FILL_PARENT));
return view;
}
}
}
layout/gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:scrollbars="horizontal" android:spacing="1dp"></Gallery>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="3"></TextView>
</LinearLayout>
I have a Gallery displaying TextViews. When it's shown, the 1st item is not displayed. This is because parent.getWidth() returns 0 at the first call.
So how can I set the item's width to be a fraction of its parent gallery's width?
Here is the code:
public class GalleryActivity extends Activity {
private Gallery gallery;
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.gallery);
gallery = (Gallery) findViewById (R.id.gallery);
gallery.setAdapter (new Adapter ());
}
private class Adapter extends BaseAdapter {
private String[] items = {"A", "B", "C", "D", "E", "F"};
public int getCount () {
return items.length;
}
public Object getItem (int position) {
return items[position];
}
public long getItemId (int position) {
return position;
}
public View getView (int position, View convertView, ViewGroup parent) {
TextView view = new TextView (GalleryActivity.this);
view.setText (getItem (position).toString ());
view.setBackgroundColor (Color.GRAY);
view.setGravity (Gravity.CENTER);
view.setLayoutParams (new Gallery.LayoutParams (parent.getWidth () / 3,
LayoutParams.FILL_PARENT));
return view;
}
}
}
layout/gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:scrollbars="horizontal" android:spacing="1dp"></Gallery>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="3"></TextView>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,不要引用我的话,但我相信在绘制某些内容之前,宽度实际上不会被设置,因此 getWidth 在此之前将始终返回 0。如果是这样,并且你知道你想要什么宽度和高度,你可以提前使用 setWidth() 和 setHeight() 手动设置宽度和高度...
如果你想要的只是实际显示屏幕的宽度和高度,你可以使用...
并且免责声明:我以前从未与画廊合作过,如果有更好的“最佳实践”方法来完成您正在尝试的事情,我不会感到惊讶...
OK don't quote me on this but I believe the width won't actually get set until some contents have been drawn, so getWidth will always return 0 before that. If so, and you know what width and height you want anyway you can manually set width and height ahead of time with setWidth() and setHeight()...
If all you want is the width and height of the actually display screen, you can use...
And disclaimer: I've never worked with Galleries before and wouldn't be surprised if there's a better, "best practice" way to do what you're trying...
我发现了问题所在:在最后一行中,
parent.getWidth()
在第一次调用时返回 0 (因为布局尚未完成),因此第一个项目没有显示......所以问题现在变成:如何将画廊项目的宽度设置为其父项宽度的一部分?困难在于 Gallery 只接受
Gallery.LayoutParams
。我现在重新表述了原来的问题。
I've found what's wrong: in the last line,
parent.getWidth()
returns 0 at the first call (because layout is not finished) so the first item is not displayed...So the question now becomes: How can I set a gallery item's width as a fraction of its parent's width? The difficulty is that Gallery only accepts
Gallery.LayoutParams
.I've now reformulated the original question.
我最近遇到了同样的问题。我通过在设置适配器后立即在画廊适配器上调用
notifyDataSetChanged()
来解决这个问题。看起来视图在画廊可见之前就已经更新了,所以我没有看到突然改变宽度带来的任何故障。onCreate()
回调中类似这样的内容:I recently ran into this same problem. I managed to solve this by calling
notifyDataSetChanged()
on the galleries adapter immediately after I set the adapter. It would seem that the view is updated before the gallery is even visible so I don't see any glitches from suddenly changing the width.Something like this in the
onCreate()
callback: