动态添加多个视图到ViewSwiper

发布于 2024-12-28 13:30:35 字数 931 浏览 1 评论 0原文

我使用以下代码将多个 TextView 添加到 CommonsWare ViewSwiper,但我看到重叠 TextView:

    for (int i = 0; i < 10; i++){

        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.c_layout, null);

        // fill in any details dynamically here
        TextView textView = (TextView) v.findViewById(R.id.myTextView);
        textView.setText("This is text " + i);

        swiper.addView(textView);

    }

以下是 c_layout.xml 的布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

执行此操作的正确方法应该是什么。

I am using the following code to add multiple TextViews to CommonsWare ViewSwiper but I see Overlapping TextViews:

    for (int i = 0; i < 10; i++){

        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.c_layout, null);

        // fill in any details dynamically here
        TextView textView = (TextView) v.findViewById(R.id.myTextView);
        textView.setText("This is text " + i);

        swiper.addView(textView);

    }

Following is the layout for c_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

what should be the proper way of doing this.

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

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

发布评论

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

评论(1

向地狱狂奔 2025-01-04 13:30:35

使用以下代码使其正常工作,以防其他人需要帮助:

    ViewSwiper swiper = (ViewSwiper)findViewById(R.id.swiper); 

    ViewFlipper flipper = swiper.getFlipper();

    for (int i = 0; i < 10; i++){

        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.trivia_text, null);

        // fill in any details dynamically here
        TextView textView = (TextView) v.findViewById(R.id.triviaText);
        textView.setId(i);
        textView.setText("This is text " + i);

        flipper.addView(textView);

    }

Used the following code to get it to work, In case someone else needs help:

    ViewSwiper swiper = (ViewSwiper)findViewById(R.id.swiper); 

    ViewFlipper flipper = swiper.getFlipper();

    for (int i = 0; i < 10; i++){

        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.trivia_text, null);

        // fill in any details dynamically here
        TextView textView = (TextView) v.findViewById(R.id.triviaText);
        textView.setId(i);
        textView.setText("This is text " + i);

        flipper.addView(textView);

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