在我的PagerAdapter中制作一个ImageView全屏 - 如何?

时间:2013-08-12 19:22:58

标签: android layout android-pageradapter

我正在使用某种类型的图库测试应用程序,我设法使用PagerAdapter创建,但我无法在全屏显示图像。

以下是一些屏幕截图:屏幕上的View1 http://imgur.com/TVvFs2v,从View1过渡到View2 http://imgur.com/h6RhxfN

我想要的是删除白色边框和操作栏,使图像全屏。

以下是代码:

SSTest.java

package com.exp.viewpagersstest1;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class SSTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sstest);

        ViewPager ssViewPager = (ViewPager) findViewById(R.id.ss_view_pager);
        ImageAdapter ssAdapter = new ImageAdapter(this);
        ssViewPager.setAdapter(ssAdapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sstest, menu);
        return true;
    }


}

ImageAdapter.java

package com.exp.viewpagersstest1;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {

    Context ssContext;

    private int[] ssImages = new int[] { R.drawable.splash1,R.drawable.splash2, R.drawable.splash3 };

    ImageAdapter(Context ssContext) {
        this.ssContext = ssContext;
    }

    @Override
    public int getCount() {
        return ssImages.length;
    }

    @Override
    public boolean isViewFromObject(View ssView, Object ssObject) {
        return ssView == ((ImageView) ssObject);
    }

    @Override
    public Object instantiateItem(ViewGroup ssContainer, int ssPosition) {
        ImageView ssImageView = new ImageView(ssContext);
        // int padding = ssContext.getResources().getDimensionPixelSize(0x7f040002);
        ssImageView.setPadding(0, 0, 0, 0);
        ssImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        ssImageView.setImageResource(ssImages[ssPosition]);
        ((ViewPager) ssContainer).addView(ssImageView, 0);
        return ssImageView;
    }

    @Override
    public void destroyItem(ViewGroup ssContainer, int ssPosition,
            Object ssObject) {
        ((ViewPager) ssContainer).removeView((ImageView) ssObject);
    }

}

activity_sstest.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SSTest" >

    <android.support.v4.view.ViewPager
        android:id="@+id/ss_view_pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true" />

</RelativeLayout>

编辑:

答案很完美,只需从xml中删除这些行就可以摆脱白边:

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

1 个答案:

答案 0 :(得分:1)

将活动样式设置为NoActionBar应该可以解决问题。例如,将您的清单中的一个放入其中:

ActionBarSherlock:

    <activity
        android:name="com.exp.viewpagersstest1.SSTest"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock.NoActionBar" >
    </activity>

原生行动栏:

    <activity
        android:name="lecho.app.campus.activity.PlacePhotoActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen
    </activity>