尝试以编程方式在ViewFlipper中创建多个相对布局

时间:2014-07-25 10:21:01

标签: android android-layout viewflipper android-relativelayout

我正在尝试以编程方式在RelativeLayouts内创建多个ViewFlipper。 我这样做是为了构建一个包含N个图像的动态ViewFlipper,N个更改。

我正在尝试创建类似的东西:(可以使用BTW ..)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/view_flipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:src="@drawable/lightning" />

<TextView
    style="@style/ImageTitle"
    android:text="@string/lightning" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:src="@drawable/color_baloons" />

<TextView
    style="@style/ImageTitle"
    android:text="@string/color_baloons" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:src="@drawable/natural_wall" />

<TextView
    style="@style/ImageTitle"
    android:text="@string/natural_wall" />

</RelativeLayout>

</ViewFlipper>

<ImageView
    android:id="@+id/swipe_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/swipe_left" />

<ImageView
    android:id="@+id/swipe_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/swipe_right" />

</RelativeLayout>

这是我的代码:

问题:我没有看到图片

我错过了什么?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_image_layout);
    _context = this;
    _viewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);

    Intent intent = getIntent();
    _imageUrls = intent.getStringArrayExtra("theImageUrl");

    for (int i = 0; i < _imageUrls.length; i++) {

        RelativeLayout layout = new RelativeLayout(_context);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        layout.setLayoutParams(layoutParams);

        ImageView imageView = new ImageView(_context);
        LayoutParams imageParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        imageView.setLayoutParams(imageParams);
        imageView.setAdjustViewBounds(true);
        imageView.setScaleType(ScaleType.CENTER_CROP);

        File file = new File(_imageUrls[i]);
        Uri uri = Uri.fromFile(file);
        final String imagePathFromUri = uri.getPath();

        final Bitmap bitmap = BitmapHelper.loadImageFromUrl(
                "file://" + _imageUrls[i],
                imagePathFromUri,
                BUFFER_IO_SIZE);

        if (bitmap != null) {

            final Drawable drawable = new BitmapDrawable(
                    getResources(),
                    bitmap);

            if (drawable != null) {
                imageView.setBackgroundDrawable(drawable);
            }
        }


        TextView textView = new TextView(_context);
        textView.setTextAppearance(_context, R.style.ImageTitle);

        layout.addView(imageView);
        layout.addView(textView);

        _viewFlipper.addView(layout);
    }

    _viewFlipper.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });
}

0 个答案:

没有答案