scrollview没有以编程方式滚动设置高度

时间:2015-09-15 21:33:20

标签: android xml scrollview android-linearlayout layoutparams

我会从我的目标开始。我想要实现的是布局比真正的手机屏幕大3倍。

现在我尝试测试我的解决方案,但是我有滚动问题。 这就是看我的布局:

<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="400dp"
        tools:context=".Zo"
        android:id="@+id/zo_root"
>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="9">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:background="@color/Red"
        ></FrameLayout >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:background="@color/Yellow"
        ></FrameLayout >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:background="@color/Blue"
        ></FrameLayout >
</LinearLayout>
</ScrollView>

我的代码背后:

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zo);
        setParameterForScroolView();   
    }

    private void setParameterForScroolView()
    {
        ParameterKeeper parameterKeeper = new ParameterKeeper(this);
        LinearLayout linearLayout = (LinearLayout)findViewById(R.id.zo_root);
        linearLayout.getLayoutParams().height = parameterKeeper.getHeightOfScreen() *3;       

    }

背后的代码效果很好,因为我的整个屏幕都是红色的,但我根本无法滚动它。

answer表示我需要从scrollView android:fillViewport="true"中删除。之后,我的LinearLayout就像this问题一样消失了。

我将LinearLayout添加为root(source),但它根本没有帮助我。

你有什么想法解决它吗?

我还尝试在height中为1600dp设置400dp LinearLayout而不是xml,以确保重新计算不会出现问题view。它也不起作用。

2 个答案:

答案 0 :(得分:0)

您正在更改错误布局的大小。你想修改滚动视图中第一个布局的高度。

答案 1 :(得分:0)

如果子元素具有layout_weight并且我在父视图中以编程方式更改高度值,则某种方式滚动不起作用。

在我的情况下,使其工作的唯一方法是在活动代码中的每个frame layout中设置高度等于屏幕大小。通过这种方式,我可以达到layout height == 3 x real screen size

的目标
相关问题