如何在Android中通过编程设置滚动视图?

如何在Android中通过编程设置滚动视图?,android,Android,我想设置滚动视图的高度、宽度、边距和设置在文本视图的下方和相对布局的右侧。我尝试只动态设置滚动视图的高度。但如何设置滚动视图的边距以及如何以编程方式将其保持在: android:layout_below="@+id/item_textInspectorName" android:layout_toRightOf="@+id/rel_out_two" 这是我的XML代码 <RelativeLayout> ................ <S

我想设置滚动视图的高度、宽度、边距和设置在文本视图的下方和相对布局的右侧。我尝试只动态设置滚动视图的高度。但如何设置滚动视图的边距以及如何以编程方式将其保持在:

        android:layout_below="@+id/item_textInspectorName"
        android:layout_toRightOf="@+id/rel_out_two"
这是我的XML代码

<RelativeLayout> ................
  <ScrollView
            android:id="@+id/scrollExpand"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/item_textInspectorName"
            android:layout_toRightOf="@+id/rel_out_two"
            android:layout_marginLeft="7.5dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="6dp"

            android:background="@android:color/white"
            android:fillViewport="true">

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

                <com.example.tazeen.classnkk.ExpandableTextView
                    android:id="@+id/expandable_text"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:ellipsize="end"
                    android:text="expand"
                    android:textColor="@android:color/black"
                    android:textSize="12sp"
                    android:textStyle="normal" />
            </LinearLayout>
        </ScrollView>

</RelativeLayout> ................
这是我的样品

MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:gravity="center">

        <TextView
            android:id="@+id/top_textview"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:text="Top Text" />

        <TextView
            android:id="@+id/left_textview"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/top_textview"
            android:text="Left Text"/>

            <ScrollView
                android:id="@+id/scrollExpand"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffffff"
                android:fillViewport="true">

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

                    <TextView
                        android:id="@+id/expandable_text"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="expand"
                        android:textColor="@android:color/black"
                        android:textSize="12sp"
                        android:textStyle="normal" />
                </LinearLayout>
            </ScrollView>
    </RelativeLayout>
这是我的样品

MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:gravity="center">

        <TextView
            android:id="@+id/top_textview"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:text="Top Text" />

        <TextView
            android:id="@+id/left_textview"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/top_textview"
            android:text="Left Text"/>

            <ScrollView
                android:id="@+id/scrollExpand"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffffff"
                android:fillViewport="true">

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

                    <TextView
                        android:id="@+id/expandable_text"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="expand"
                        android:textColor="@android:color/black"
                        android:textSize="12sp"
                        android:textStyle="normal" />
                </LinearLayout>
            </ScrollView>
    </RelativeLayout>

谢谢你的回答。这对我很有帮助。谢谢你的回答。这对我很有帮助。
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollExpand);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
    params.setMargins(20, 20, 20, 20);
    params.addRule(RelativeLayout.BELOW, R.id.top_textview);
    params.addRule(RelativeLayout.RIGHT_OF, R.id.left_textview);
    scrollView.setLayoutParams(params);