Android ViewPager维度
Android ViewPager维度,android,android-layout,android-viewpager,Android,Android Layout,Android Viewpager,ViewPager是否必须是活动布局中唯一的对象? 我正在尝试实现如下内容: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/reader_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &
ViewPager
是否必须是活动布局中唯一的对象?
我正在尝试实现如下内容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reader_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/page_viewer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Gallery
android:id="@+id/miniatures_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" /></LinearLayout>
我应该在哪里有一个大的寻呼机在上面滚动(我有它),在下面有一个小的图库滚动。
这只显示传呼机,不显示图库。
有什么建议吗?ViewPager不支持
wrap\u内容
,因为它(通常)从不同时加载所有子项,因此无法获得适当的大小(选项是在每次切换页面时都有一个改变大小的寻呼机)
但是,您可以设置精确的尺寸(例如150dp),并且match\u parent
也可以工作。您还可以通过更改其
LayoutParams
中的height
-属性,从代码中动态修改尺寸。我通过一些技巧解决了这个问题。它涉及的内容如下:
首先,我需要一个可以忽略ViewPager
高度限制的布局。将其用作ViewPager
项目的父布局
public class TallLinearLayout extends LinearLayout {
public TallLinearLayout(Context context) {
super(context);
}
public TallLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TallLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
}
}
然后,我编写了调整ViewPager大小的逻辑:
private class ViewPagerContentWrapper implements OnGlobalLayoutListener {
private ViewPager mViewPager;
public ViewPagerContentWrapper(ViewPager viewPager) {
mViewPager = viewPager;
}
@Override
public void onGlobalLayout() {
int position = mViewPager.getCurrentItem();
check(position);
check(position + 1);
}
private void check(int position) {
ViewGroup vg = (ViewGroup) mViewPager.getChildAt(position);
View v = vg == null ? null : vg.getChildAt(0);
if (v != null) {
int height = v.getHeight();
if (height > mViewPager.getHeight()) {
resize(height);
}
}
}
private void resize(int height) {
mViewPager.setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
height
)
);
}
}
我注册为全局布局侦听器:
viewPager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewPagerContentWrapper(viewPager));
分配版面权重,以将寻呼机视为1&高度=0dp,而不是换行内容
<android.support.v4.view.ViewPager
android:id="@+id/page_viewer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
什么东西不能使用它?另一个解决方案是,如果你想让寻呼机填满任何空白,那么它的工作原理是:然后寻呼机为任何其他视图留出足够的空间。试试这个:tnx这很好…但是我在页面旋转方面遇到了问题,有什么建议吗?你有堆栈跟踪吗?您是从github抓取项目还是从教程中复制粘贴?更新的helpmeco.de link可能重复这将创建一个无限的布局周期。最初,系统请求onLayout调用OnGloballYoutListener的onLayout调用OnGloballYoutListener的onLayout等等…如何在RelativeLayout上实现?