您的位置:首页 > 移动开发 > Android开发

android中解决viewpager控件自动填满屏幕问题

2014-11-19 10:46 190 查看
在android开发中页面布局中会使用viewpager控件来做有关图片的项目,viewpager之上的控件不会受到它的影响,但是viewpager会自动填满剩下的屏幕空间,所以在viewpager之下的控件不会显示!在和度娘亲切交流了很久后找到了一个解决办法。

xml文件,删掉了部分代码,但是大体思想很清楚,代码如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/vp_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

<TextView
android:id="@+id/testview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/news"
android:gravity="center"
android:textSize="30px"/>
</LinearLayout>
</ScrollView>
xml文件中为viewpager添加了一个linearlayout作为父控件,在java代码中就可以通过限制viewpager父控件的大小来限制viewpager的大小

java代码如下:
<span style="white-space:pre">		</span>layout=(LinearLayout)findViewById(R.id.linear1);
adViewPager=(ViewPager)findViewById(R.id.vp_main);
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) layout.getLayoutParams();
params.height=dm.heightPixels/2; //设置父控件高度为屏幕高度的一半
params.width=dm.widthPixels; //设置父控件宽度为屏幕宽度
layout.setLayoutParams(params);
这样问题就解决了,感谢大神提供思路!!! 点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐