如何让 ViewPager 全屏显示?
How to make ViewPager fullscreen?
我有一个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):
编辑: 我基本上想做一个迷你教程。一页有图片,一页有电影。你认为最好的方法是什么?
我们的代码
android:layout_width="fill_parent"
android:layout_height="fill_parent"
改为
android:layout_width="match_parent"
android:layout_height="match_parent"
或者您可以尝试将此添加到您的 ViewPager
android:layout_height="0dp"
android:layout_weight="1"
让我知道是否存在其他问题
-现在我看到你的台词了
LayoutParams lp = new LayoutParams(width, height);
将其更改为以下代码:-
Layoutparams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
试试这个:
<LinearLayout 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:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
/>
</LinearLayout>
试试这个:
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" />
我觉得很愚蠢...请注意,在我的 onCreate 中我有 getWindow().setLayout(Main.screenWidth, Main.screenHeight)
烦人的是我将 Main.screenHeight)
设置为 75% :( 删除它解决了我的问题。
我有一个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):
编辑: 我基本上想做一个迷你教程。一页有图片,一页有电影。你认为最好的方法是什么?
我们的代码
android:layout_width="fill_parent"
android:layout_height="fill_parent"
改为
android:layout_width="match_parent"
android:layout_height="match_parent"
或者您可以尝试将此添加到您的 ViewPager
android:layout_height="0dp"
android:layout_weight="1"
让我知道是否存在其他问题
-现在我看到你的台词了
LayoutParams lp = new LayoutParams(width, height);
将其更改为以下代码:-
Layoutparams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
试试这个:
<LinearLayout 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:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
/>
</LinearLayout>
试试这个:
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" />
我觉得很愚蠢...请注意,在我的 onCreate 中我有 getWindow().setLayout(Main.screenWidth, Main.screenHeight)
烦人的是我将 Main.screenHeight)
设置为 75% :( 删除它解决了我的问题。