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

来源:互联网 发布:kingroot网络连接失败 编辑:程序博客网 时间:2024/05/08 21:16

在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);
这样问题就解决了,感谢大神提供思路!!! 点击打开链接

0 0
原创粉丝点击