Android ViewPager 不支持 layout_height = wrap_content
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10910669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ViewPager not supporting layout_height = wrap_content
提问by Andrei Stalbe
I'm building an android application with the following layout.
我正在构建一个具有以下布局的 android 应用程序。
It's already known that layout_height="wrap_content"
doesn't work for the ViewPager.
众所周知,这layout_height="wrap_content"
不适用于 ViewPager。
My question is how to change it dynamically. The content of the ViewPager is inflated from two xml layouts, vp_statistics.xml and vp_campaigns.xml.
我的问题是如何动态更改它。ViewPager 的内容从两个 xml 布局膨胀,vp_statistics.xml and vp_campaigns.xml.
here's the xml for the ViewPager
这是 ViewPager 的 xml
<LinearLayout
android:id="@+id/topcontent_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/viewpager_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Here's the code for my PagerAdapter
这是我的 PagerAdapter 的代码
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
public Object instantiateItem(View collection, int pos){
LayoutInflater inflater = (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch(pos){
case 0:
resId = R.layout.vp_statistics;
break;
case 1:
resId = R.layout.vp_campaigns;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager)collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager)container).removeView((View)object);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == ((View)arg1);
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
}
and inside my onCreate() mthod of the Activity class:
在 Activity 类的 onCreate() 方法中:
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
Is there a way to get the height of the vp_statistics.xml
and vp_campaigns.xml
layouts and assign it dynamically to ViewPager (@id/viewpager)
every time the instantiateItem()
object gets called?
有没有办法在每次调用对象时获取vp_statistics.xml
和vp_campaigns.xml
布局的高度并动态分配它?ViewPager (@id/viewpager)
instantiateItem()
回答by josephus
- Add a
weightSum=2
attribute to the parentLinearLayout
, and setorientation=vertical
. - Set the top child LinearLayout's
layout_weight=1
. Also, setlayout_height=0dip
- Wrap the ViewPager into another
LinearLayout
withlayout_weight=1
andlayout_height=0dip
weightSum=2
向 parent添加一个属性LinearLayout
,并设置orientation=vertical
.- 设置顶级孩子 LinearLayout 的
layout_weight=1
. 另外,设置layout_height=0dip
LinearLayout
用layout_weight=1
和将 ViewPager 包装成另一个layout_height=0dip
回答by cybergen
回答by virusss8
use 0dp
instead of wrap_content
, and for dynamically change that
使用0dp
代替wrap_content
, 并动态更改它
pager = new ViewPager(this);
pager.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
回答by OceanLife
In order to assign different screen real estate to each linear layout try using the layout_weight
parameter. You can reassign this in-code to achieve this 'dynamic' weighting change. Overview herewith plenty of Google-able examples of how to achieve the weight layouts in code...
为了为每个线性布局分配不同的屏幕空间,请尝试使用该layout_weight
参数。您可以重新分配此代码以实现此“动态”权重更改。此处概述了大量有关如何在代码中实现权重布局的 Google 示例...
回答by franta kocourek
Use RelativeLayout:
使用相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/layout" >
</android.support.v4.view.ViewPager>
</RelativeLayout>