java - 如何让 ViewPager 全屏显示?

标签 java android android-viewpager

我有一个ViewPager,我想全屏显示,但它只显示了一部分。我怎样才能让它全屏显示?这是我的代码,请注意它充满了使布局全屏显示的许多调用,但无济于事。

walkthrough_activity.xml:

<?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.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:minWidth="1080dp"
        android:minHeight="1920dp"/>       
    </RelativeLayout>

walkthrough_single_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

WalkthroughActivity.java:

package org.core.game.activities;

import org.core.game.Log;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class WalkthroughActivity extends Activity
{
    private static final int MAX_VIEWS = 1;

    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.walkthrough_activity);
        getWindow().setLayout(Main.screenWidth, Main.screenHeight);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager.setAdapter(new WalkthroughPagerAdapter());
        mViewPager.setOnPageChangeListener(new WalkthroughPageChangeListener());
        mViewPager.setX(0);
        mViewPager.setY(0);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = width;

        LayoutParams lp = new LayoutParams(width, height);
        mViewPager.setLayoutParams(lp);         
    }

    class WalkthroughPagerAdapter extends PagerAdapter
    {
        @Override
        public int getCount()
        {
            return MAX_VIEWS;
        }

        @Override
        public boolean isViewFromObject(View view, Object object)
        {
            return view == (View) object;
        }

        @SuppressLint("InflateParams")
        @Override
        public Object instantiateItem(View container, int position)
        {
            Log.e("instantiateItem(" + position + ");");
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View imageViewContainer = inflater.inflate(R.layout.walkthrough_single_view, null);
            ImageView imageView = (ImageView) imageViewContainer.findViewById(R.id.image_view);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setAdjustViewBounds(true);

            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = width;
            imageView.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.FILL_PARENT));

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(1080, 1920);

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(1080, 1920);
            imageView.setLayoutParams(layoutParams);

            switch (position)
            {
                case 0:
                    imageView.setImageResource(R.drawable.should_be_full_screen);
                    break;          
            }

            ((ViewPager) container).addView(imageViewContainer, 0);
            return imageViewContainer;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            ((ViewPager) container).removeView((View) object);
        }
    }
}

这是它的外观(图像为 1080*1920):

And this is how it looks:

编辑:我基本上想做一个迷你教程。一页有图片,一页有电影。您认为最好的方法是什么?

最佳答案

试试这个:

int deviceWidthInPixels = getResources().getDisplayMetrics().widthPixels;
int deviceHeightInPixels = getResources().getDisplayMetrics().heightPixels;
imageView.getLayoutParams().width = deviceWidthInPixels;
imageView.getLayoutParams().height = deviceHeightInPixels;

此外,将此用于 ViewPager:

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" /> 

这是 ImageView

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY" />

关于java - 如何让 ViewPager 全屏显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29315746/

相关文章:

java - HotSpot 堆栈防护页。红色/黄色区域及其背后的原因

java - Controller 和 Wicket 页面的设计模式帮助

android - 嵌套线性布局权重

java - ViewPager PagerAdapter 不更新 View

android - 如何禁用背后 View 点击事件Framelayout

Java MySQL Timestamp时区问题

android - 如何从 Windows Phone 8 中的特定提供商获取位置信息?

android - 在 Android 5.0 上的 AppCompat CardView 上以 XML 设置海拔

android - 嵌套的 ViewPager 在 onBackPressed() 上空白内容

Java - Arraylist 实现存疑