如何使RelativeLayout可滚动

时间:2016-02-17 14:40:37

标签: android android-layout

我正在尝试在我的Android应用程序中制作自定义视图。 这是我布局的一部分:

<com.kennyc.view.MultiStateView
    android:id="@+id/multiStateView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:msv_errorView="@layout/view_error"
    app:msv_loadingView="@layout/view_loading"
    app:msv_viewState="loading">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.intech.mishiko.views.stats.WeekHistogramView
            android:id="@+id/chart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/mean"
            android:layout_alignParentTop="true" />

        <com.intech.mishiko.views.stats.MeanActivityView
            android:id="@+id/mean"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:padding="@dimen/activity_margin_half" />
    </RelativeLayout>

</com.kennyc.view.MultiStateView>

WeekHistogramView扩展了View类:

public class WeekHistogramView extends BaseHistogramView {
    //my code

    @Override
    onDraw() {
        //custom drawing
    }
}

MeanActivityView是一个LinearLayout:

public class MeanActivityView extends LinearLayout {
    //my code
    //set and show some text fields.
}  

一切正常。

ui works correct

但是新的重新说法我需要可滚动的行为。 我在我的代码中添加了ScrollView:

<com.kennyc.view.MultiStateView
    android:id="@+id/multiStateView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:msv_errorView="@layout/view_error"
    app:msv_loadingView="@layout/view_loading"
    app:msv_viewState="loading">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.intech.mishiko.views.stats.WeekHistogramView
                android:id="@+id/chart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/mean"
                android:layout_alignParentTop="true" />

            <com.intech.mishiko.views.stats.MeanActivityView
                android:id="@+id/mean"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:padding="@dimen/activity_margin_half" />
        </RelativeLayout>
    </ScrollView>
</com.kennyc.view.MultiStateView>

但是在那之后WeekHistogramView返回0高度。它是看不见的。

我尝试在ScrollView中包装MultiStateView但没有任何改变。

之后我改变了RelativeLayout - &gt;垂直LinearLayout,但也不起作用。

ui works incorrect

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

RelativeLayout换成LinearLayout,以便ScrollView的唯一孩子为LinearLayout。这样就可以了。

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

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

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

          //....

        </RelativeLayout>
    </LinearLayout>
</ScrollView>
相关问题