我有一个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):
编辑:我基本上想做一个迷你教程。一页有图像,一页有电影。您认为最好的方式是什么?
答案 0 :(得分:1)
试试这个:
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" />
答案 1 :(得分:0)
我们的代码
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);
答案 2 :(得分:0)
试试这个:
<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>
答案 3 :(得分:0)
我觉得很愚蠢......请注意,在我的onCreate中我getWindow().setLayout(Main.screenWidth, Main.screenHeight)
令我恼火的是Main.screenHeight)
为75%:(删除它解决了我的问题。