gpt4 book ai didi

java - Fragment Android 中 ScrollView 内的页面 View

转载 作者:搜寻专家 更新时间:2023-11-01 08:22:20 25 4
gpt4 key购买 nike

我有一个 android fragment 布局,我想将我的页面 View 放在 ScrollView 中。当应用程序在没有 ScrollView 的情况下运行时,它显示了图像,但是当我将页面 View 代码放在 ScrollView 中时,它没有显示图像。

这是我的代码

<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"
tools:context="com.example.wk.sigah.FragmentHome"
android:background="@color/colorPrimary">


<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
android:id="@+id/viewImage"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>

</ScrollView>

</LinearLayout>

最佳答案

Override onMeasure in class and extends ViewPager as follows, this will make it get the height of the biggest child it currently has.

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

public class CustomViewPager extends ViewPager {

public CustomViewPager(Context context) {
super(context);
}

public CustomViewPager(Context context, AttributeSet attrs){

super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int height = 0;
for(int i = 0; i < getChildCount(); i++) {

View child = getChildAt(i);

child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

int h = child.getMeasuredHeight();

if(h > height) height = h;

}

heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

在 XML 中:

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:fillViewport="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.your.package.name.CustomViewPager
android:id="@+id/viewImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

Tutorial

关于java - Fragment Android 中 ScrollView 内的页面 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49489174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com