Java android - 动态设置 ScrollView 高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18580279/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:11:19  来源:igfitidea点击:

android - Setting ScrollView height dynamically

javaandroideclipse

提问by Dênis Montone

I have a layout wich is nested like this:

我有一个这样嵌套的布局:

<RelativeLayout 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" >

    <ScrollView 
        android:id="@+id/scrollAplicacoes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin" >

            <TextView 
                android:id="@+id/titulo"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:fontFamily="Arial"
                android:lineSpacingExtra="-5dp"
                android:text="@string/titulo_aplicacao"
                android:textColor="#3c3c3c"
                android:textSize="25sp"
                android:textStyle="bold"/>

            <ImageView
                android:id="@+id/btnAplicacaoUm"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:onClick="selectAplicacao"
                android:src="@drawable/aplicacoes_item1" />

            <ImageView
                android:id="@+id/btnAplicacaoDois"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_marginTop="10dp"
                android:onClick="selectAplicacao"
                android:src="@drawable/aplicacoes_item2" />

            <ImageView
                android:id="@+id/btnAplicacaoTres"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_marginTop="10dp"
                android:onClick="selectAplicacao"
                android:src="@drawable/aplicacoes_item3" />

        </LinearLayout>

    </ScrollView>

    <LinearLayout 
        android:id="@+id/menuFixo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/faixa_below" >

        <ImageView
            android:id="@+id/belowHome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:contentDescription="@string/descBtnHome"
            android:layout_marginTop="5dp"
            android:src="@drawable/below_home_sel" />

        <ImageView
            android:id="@+id/belowCalculadora"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="abreCalculadora"
            android:contentDescription="@string/descBtnCalculadora"
            android:layout_marginTop="5dp"
            android:src="@drawable/below_calc" />

        <ImageView
            android:id="@+id/belowProdutos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/descFaixaProdutos"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:src="@drawable/below_produtos" />

    </LinearLayout>

</RelativeLayout>

The problem is that the LinearLayout outside the ScrollView, at the end is fixed on the bottom, and the scroll is placed behind it. So i need the Scroll to be the screen height minus this fixed LinerLayout height minus a little margin.

问题是ScrollView外面的LinearLayout,最后固定在底部,滚动条放在它后面。所以我需要 Scroll 是屏幕高度减去这个固定的 LinerLayout 高度减去一点边距。

I tried this:

我试过这个:

(...)
int scrollFInalHeight = scrollHeight - fixedMenuHeight - 10;

ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(scrollStartWidth, scrollFInalHeight);
scroll.setLayoutParams(param);

But the app crashes when I start this activity. This java code is within the public void onWindowFocusChanged(boolean hasFocus)method.

但是当我开始这个活动时应用程序崩溃了。此 java 代码位于public void onWindowFocusChanged(boolean hasFocus)方法中。

Any suggestions? Thanks in advance! ;)

有什么建议?提前致谢!;)

采纳答案by Enrichman

Have you tried to put

你有没有试过把

android:layout_above="@+id/menuFixo"

in your ScrollView?

在你的滚动视图中?

回答by aNiKeT

You need to set Height

您需要设置高度

scroll_view.setLayoutParams(new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

or

或者

scroll_view.setLayoutParams(new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, 350));

回答by thanhbinh84

@aNikeT's answer is correct, but it will be safer with

@aNikeT 的答案是正确的,但使用它会更安全

ViewGroup.LayoutParams layoutParams = mDescriptionScrollView.getLayoutParams();
            layoutParams.height = scrollViewHeight;