ViewFlipper 具有可变数量的线性布局
在我的应用程序中,我需要使用 ViewFlipper 通过在触摸屏上拖动来切换屏幕。我有一个包含一些项目的 ListView。当选择一个项目时,我必须显示一些图像。问题在于,每一项的图像数量不同。所以我认为我必须以编程方式创建 LinearLayout 和 ImageView 。我想做的事情可能吗?
预先感谢..
这是我尝试过的代码:
<?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" android:background="#000000"
android:id="@+id/layout_main">
<ViewFlipper android:id="@+id/details"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</ViewFlipper>
</LinearLayout>
以及 Oncreate() 方法:
for (int i = 0; i < jArray.length(); i++) {
LinearLayout l=new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
l.setBackgroundColor(0xFFFFFFFF);
l.setOrientation(LinearLayout.VERTICAL);
ImageView img = new ImageView(this);
img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//ImageView img1 = (ImageView) findViewById(R.id.photo1);
Drawable image1 = ImageOperations(getBaseContext(), url[i]);
img.setImageDrawable(image1);
l.addView(img);
}
但没有任何反应。我只看到一个大黑屏。
In my app I need to use ViewFlipper for Switching screens by dragging over the touch screen. I have a ListView that contains some items. When a item is select I must to display some images. The problem is that the number of images is different from one item to another. So I think that I must create the LinearLayout and the ImageView programatically. Is it possible what I am tring to do?
Thanks in advance..
Here is the code I tried :
<?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" android:background="#000000"
android:id="@+id/layout_main">
<ViewFlipper android:id="@+id/details"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</ViewFlipper>
</LinearLayout>
and on Oncreate() method :
for (int i = 0; i < jArray.length(); i++) {
LinearLayout l=new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
l.setBackgroundColor(0xFFFFFFFF);
l.setOrientation(LinearLayout.VERTICAL);
ImageView img = new ImageView(this);
img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//ImageView img1 = (ImageView) findViewById(R.id.photo1);
Drawable image1 = ImageOperations(getBaseContext(), url[i]);
img.setImageDrawable(image1);
l.addView(img);
}
but nothing happens. I see only a big black screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我完全不明白你在说什么,但我以前在 ViewFlipper 上工作过。在 xml 中使用四个线性布局作为 ViewFlipper 的子视图。 (首先我尝试直接添加列表视图作为子视图,但添加线性布局会更容易)
并添加列表视图,图像视图,就像你所说的从java代码中相应的线性布局。
它奏效了。
希望有帮助。
这是我用于 xml 的代码:
之后您可以从 xml 获取线性布局的 ID,并从 java 添加任何您想要的列表视图等..,,
I exactly didnt understand what you saying but i worked on ViewFlipper before. There am using Four linear layouts as childviews for ViewFlipper which in xml. (where as firstly i tried to add listviews direclly as childviews but adding linearlayouts will make easier)
and added listviews,imageviews like you said to corresponding linearlayouts from java code.
and it worked .
Hope it helps.
Here my code i used for xml :
after that you can get Ids of linearlayouts from xml and add whaterver you want listviews etc from java..,,