如何使用具有三种布局的视图翻转器?

发布于 2024-11-27 01:36:28 字数 165 浏览 1 评论 0原文

我目前正在使用 ViewFlipper 进行我的主要活动,具有两种不同的布局。我想使用第三种布局,但只能找到 showNext()showPrevious() 命令。有人可以告诉我如何使用 ViewFlipper 实现第三种布局吗?

I am currently using ViewFlipper for my main activity with two different layouts. I want to use a third layout, but I can only find the showNext() and showPrevious() commands. Can someone show me how to implement a third layout using ViewFlipper?

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

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

发布评论

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

评论(4

我们只是彼此的过ke 2024-12-04 01:36:28

为您制作了一个示例,展示如何在 ViewFlipper 中显示不同的视图。

该示例的布局由以下部分组成。有三个单选按钮。 ViewFlipper 放置在单选按钮下方。该脚蹼拥有三个不同的简单视图和不同的文本。

然后,单选按钮连接到 java 代码中的侦听器,该侦听器将根据当前选择的单选按钮更改 ViewFlipper 显示的视图。

XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <RadioGroup android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton android:layout_height="wrap_content"
            android:id="@+id/radio0" android:layout_width="wrap_content"
            android:text="Show View 1" android:checked="true"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:id="@+id/radio1" android:layout_width="wrap_content"
            android:text="Show view 2"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:id="@+id/radio2" android:layout_width="wrap_content"
            android:text="Show View 3"></RadioButton>
    </RadioGroup>

    <ViewFlipper android:id="@+id/ViewFlipper01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <!--adding views to ViewFlipper-->
        <TextView android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="First view is now displayed"></TextView>
        <TextView android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second view is now displayed"></TextView>
        <TextView android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Third view is now displayed"></TextView>
    </ViewFlipper>

</LinearLayout>

JAVA

package com.test.threeviews;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.ViewFlipper;

public class ThreeViewsinaFlipperActivity extends Activity {

    RadioButton RB0;
    RadioButton RB1;
    RadioButton RB2;
    ViewFlipper VF;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /*
         * Find the views declared in main.xml.
         */
        RB0 = (RadioButton) findViewById(R.id.radio0);
        RB1 = (RadioButton) findViewById(R.id.radio1);
        RB2 = (RadioButton) findViewById(R.id.radio2);
        VF = (ViewFlipper) findViewById(R.id.ViewFlipper01);

        /*
         * Set a listener that will listen for clicks on the radio buttons and
         * perform suitable actions.
         */
        RB0.setOnClickListener(radio_listener);
        RB1.setOnClickListener(radio_listener);
        RB2.setOnClickListener(radio_listener);
    }

    /*
     * Define a OnClickListener that will change which view that is displayed by
     * the ViewFlipper
     */
    private OnClickListener radio_listener = new OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.radio0:
                VF.setDisplayedChild(0);
                break;
            case R.id.radio1:
                VF.setDisplayedChild(1);
                break;
            case R.id.radio2:
                VF.setDisplayedChild(2);
                break;
            }
        }
    };
}

Made an example for you that shows howto display different views in a ViewFlipper.

The layout of the example is made up of the following parts. There are three radio buttons. A ViewFlipper is placed below the radio buttons. This flipper holds three different simple views with different texts.

The radio buttons are then hooked up to a listener in the java code that will change the view displayed by the ViewFlipper depending on which radio button that currently is chosen.

XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <RadioGroup android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton android:layout_height="wrap_content"
            android:id="@+id/radio0" android:layout_width="wrap_content"
            android:text="Show View 1" android:checked="true"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:id="@+id/radio1" android:layout_width="wrap_content"
            android:text="Show view 2"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:id="@+id/radio2" android:layout_width="wrap_content"
            android:text="Show View 3"></RadioButton>
    </RadioGroup>

    <ViewFlipper android:id="@+id/ViewFlipper01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <!--adding views to ViewFlipper-->
        <TextView android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="First view is now displayed"></TextView>
        <TextView android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second view is now displayed"></TextView>
        <TextView android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Third view is now displayed"></TextView>
    </ViewFlipper>

</LinearLayout>

JAVA

package com.test.threeviews;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.ViewFlipper;

public class ThreeViewsinaFlipperActivity extends Activity {

    RadioButton RB0;
    RadioButton RB1;
    RadioButton RB2;
    ViewFlipper VF;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /*
         * Find the views declared in main.xml.
         */
        RB0 = (RadioButton) findViewById(R.id.radio0);
        RB1 = (RadioButton) findViewById(R.id.radio1);
        RB2 = (RadioButton) findViewById(R.id.radio2);
        VF = (ViewFlipper) findViewById(R.id.ViewFlipper01);

        /*
         * Set a listener that will listen for clicks on the radio buttons and
         * perform suitable actions.
         */
        RB0.setOnClickListener(radio_listener);
        RB1.setOnClickListener(radio_listener);
        RB2.setOnClickListener(radio_listener);
    }

    /*
     * Define a OnClickListener that will change which view that is displayed by
     * the ViewFlipper
     */
    private OnClickListener radio_listener = new OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.radio0:
                VF.setDisplayedChild(0);
                break;
            case R.id.radio1:
                VF.setDisplayedChild(1);
                break;
            case R.id.radio2:
                VF.setDisplayedChild(2);
                break;
            }
        }
    };
}
森末i 2024-12-04 01:36:28

请参阅 android.widget.ViewFlipper 的简单用法。有了它,您可以从 xml 创建不同的布局,然后使用简单的方法在它们之间切换,如下所示:

   ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);

   // you can switch between next and previous layout and display it
   viewFlipper.showNext();
   viewFlipper.showPrevious();

  // or you can switch selecting the layout that you want to display
  viewFlipper.setDisplayedChild(1);
  viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.secondLayout)

带有树布局的 Xml 示例:

     <ViewFlipper
            android:id="@+id/myViewFlipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/firstLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
               [...]
            </LinearLayout>

            <LinearLayout
                android:id="@+id/secondLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
               [...]
            </LinearLayout>

            <LinearLayout
                android:id="@+id/thirdLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
              [...]
            </LinearLayout>
      </ViewFlipper>

See this simple use of android.widget.ViewFlipper. With it you can create different layout from xml and then switch among them with simple method like this:

   ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);

   // you can switch between next and previous layout and display it
   viewFlipper.showNext();
   viewFlipper.showPrevious();

  // or you can switch selecting the layout that you want to display
  viewFlipper.setDisplayedChild(1);
  viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.secondLayout)

Xml example with tree layouts:

     <ViewFlipper
            android:id="@+id/myViewFlipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/firstLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
               [...]
            </LinearLayout>

            <LinearLayout
                android:id="@+id/secondLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
               [...]
            </LinearLayout>

            <LinearLayout
                android:id="@+id/thirdLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
              [...]
            </LinearLayout>
      </ViewFlipper>
苏辞 2024-12-04 01:36:28

您也可以通过这种方式使用它。我已附上 java 代码和 xml 文件,其中使用按钮来更改脚蹼视图。

package com.nikhil.play.add_subtract;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ViewFlipper;

public class Flipper extends Activity implements OnClickListener {

    ViewFlipper flippy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flipper);
        flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
        flippy.setOnClickListener(this);
        flippy.setFlipInterval(10000);
        flippy.startFlipping();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        flippy.showNext();
    }

}

XML 代码-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flipper 2" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flipper 3" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flipper 4" />
    </ViewFlipper>

</LinearLayout>

You can use it in this way also. I have attached the java code and xml file where a button is used to change the flipper view.

package com.nikhil.play.add_subtract;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ViewFlipper;

public class Flipper extends Activity implements OnClickListener {

    ViewFlipper flippy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flipper);
        flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
        flippy.setOnClickListener(this);
        flippy.setFlipInterval(10000);
        flippy.startFlipping();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        flippy.showNext();
    }

}

XML CODE-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flipper 2" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flipper 3" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flipper 4" />
    </ViewFlipper>

</LinearLayout>
很快妥协 2024-12-04 01:36:28

语言

<ViewFlipper
    android:id="@+id/viewflip"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_weight="1"
    />

Java

public class BlankFragment extends Fragment{
ViewFlipper viewFlipper;
FragmentManager fragmentManager;

int gallery_grid_Images[]={drawable.image1, drawable.image2, drawable.image3,
        drawable.image1, drawable.image2, drawable.image3, drawable.image1,
        drawable.image2, drawable.image3, drawable.image1
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootView = inflater.inflate(fragment_blank, container, false);
    viewFlipper =(ViewFlipper)rootView.findViewById(R.id.viewflip);

    for(int i=0;i<gallery_grid_Images.length;i++)
    {
        //  This will create dynamic image view and add them to ViewFlipper
        setFlipperImage(gallery_grid_Images[i]);

    }

    return rootView;

}

private void setFlipperImage(int res) {
    Log.i("Set Filpper Called", res+"");
    ImageView image = new ImageView(getContext());
    image.setBackgroundResource(res);
    viewFlipper.addView(image);
    viewFlipper.setFlipInterval(1000);
    viewFlipper.setAutoStart(true);

}

xml

<ViewFlipper
    android:id="@+id/viewflip"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_weight="1"
    />

Java

public class BlankFragment extends Fragment{
ViewFlipper viewFlipper;
FragmentManager fragmentManager;

int gallery_grid_Images[]={drawable.image1, drawable.image2, drawable.image3,
        drawable.image1, drawable.image2, drawable.image3, drawable.image1,
        drawable.image2, drawable.image3, drawable.image1
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootView = inflater.inflate(fragment_blank, container, false);
    viewFlipper =(ViewFlipper)rootView.findViewById(R.id.viewflip);

    for(int i=0;i<gallery_grid_Images.length;i++)
    {
        //  This will create dynamic image view and add them to ViewFlipper
        setFlipperImage(gallery_grid_Images[i]);

    }

    return rootView;

}

private void setFlipperImage(int res) {
    Log.i("Set Filpper Called", res+"");
    ImageView image = new ImageView(getContext());
    image.setBackgroundResource(res);
    viewFlipper.addView(image);
    viewFlipper.setFlipInterval(1000);
    viewFlipper.setAutoStart(true);

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