ViewFlipper 具有可变数量的线性布局

发布于 2024-12-13 00:51:39 字数 1488 浏览 6 评论 0原文

在我的应用程序中,我需要使用 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 技术交流群。

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

发布评论

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

评论(1

悲凉≈ 2024-12-20 00:51:39

我完全不明白你在说什么,但我以前在 ViewFlipper 上工作过。在 xml 中使用四个线性布局作为 ViewFlipper 的子视图。 (首先我尝试直接添加列表视图作为子视图,但添加线性布局会更容易)

并添加列表视图,图像视图,就像你所说的从java代码中相应的线性布局。

它奏效了。

希望有帮助。

这是我用于 xml 的代码:

<RelativeLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tabrelay"
                android:gravity="center_horizontal"

                >      

    <ViewFlipper android:id="@+id/details" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        style="@style/FlipperMargins.flipper"
        >     


    <LinearLayout  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:id="@+id/list1"
    android:listSelector="@color/sprgreen"
    android:drawSelectorOnTop="true"
    android:background="@drawable/deckbackn"
    style="@style/Webview.view"

    />
     <LinearLayout 
    android:layout_width="@dimen/webViewWidth" 
    android:layout_height="@dimen/webViewHeight"
    android:layout_gravity="center"
    android:divider="@color/grey46"
    android:dividerHeight="1dp"
    android:id="@+id/list2"
    android:background="@drawable/white"
    style="@style/Webview.view"

    />
     <LinearLayout  
    android:layout_width="@dimen/webViewWidth" 
    android:layout_height="@dimen/webViewHeight"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:id="@+id/list3"
    android:listSelector="@color/sprgreen"
    android:drawSelectorOnTop="true"
    android:background="@drawable/commentsbg"
    style="@style/Webview.view"

    />

   <WebView 
   android:id="@+id/helloWebView" 
   android:layout_width="@dimen/webViewWidth" 
   android:layout_height="@dimen/webViewHeight"
   android:layout_gravity="center"
   android:background="@drawable/white"
   style="@style/Webview.view"
   /> 
   </ViewFlipper>
   </RelativeLayout>

之后您可以从 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 :

<RelativeLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tabrelay"
                android:gravity="center_horizontal"

                >      

    <ViewFlipper android:id="@+id/details" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        style="@style/FlipperMargins.flipper"
        >     


    <LinearLayout  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:id="@+id/list1"
    android:listSelector="@color/sprgreen"
    android:drawSelectorOnTop="true"
    android:background="@drawable/deckbackn"
    style="@style/Webview.view"

    />
     <LinearLayout 
    android:layout_width="@dimen/webViewWidth" 
    android:layout_height="@dimen/webViewHeight"
    android:layout_gravity="center"
    android:divider="@color/grey46"
    android:dividerHeight="1dp"
    android:id="@+id/list2"
    android:background="@drawable/white"
    style="@style/Webview.view"

    />
     <LinearLayout  
    android:layout_width="@dimen/webViewWidth" 
    android:layout_height="@dimen/webViewHeight"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:id="@+id/list3"
    android:listSelector="@color/sprgreen"
    android:drawSelectorOnTop="true"
    android:background="@drawable/commentsbg"
    style="@style/Webview.view"

    />

   <WebView 
   android:id="@+id/helloWebView" 
   android:layout_width="@dimen/webViewWidth" 
   android:layout_height="@dimen/webViewHeight"
   android:layout_gravity="center"
   android:background="@drawable/white"
   style="@style/Webview.view"
   /> 
   </ViewFlipper>
   </RelativeLayout>

after that you can get Ids of linearlayouts from xml and add whaterver you want listviews etc from java..,,

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