使RelativeLayout可滚动

时间:2016-11-16 15:45:33

标签: java android nested scrollview relativelayout

我想创建一个可滚动的RelativeLayout,我可以创建具有x和y位置以及自定义宽度和高度的按钮。 这是我到目前为止获得的代码XML:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/buttonDel"
    android:fillViewport="true">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/RL">
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

爪哇:

RelativeLayout linearLayout;

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

    linearLayout = (RelativeLayout) findViewById(R.id.RL);
    addButton();
}

private void addButton() {

    for(int i = 0; i < 20; i++)
        for(int j = 0; j < 4; j++)
        {
            Button but = new Button(this);
            but.setX(j * 200);
            but.setY(i * 200);
            but.setText("B" + i);
            linearLayout.addView(but, 200, 200);
        }
    }

除了滚动部分外,它会产生我想要的东西。我不知道为什么ScrollView不起作用。

Screen so far.

After I changed ScrollView layout_height="match_parent" to "warp_content"

2 个答案:

答案 0 :(得分:1)

从&#34; ScrollView&#34;更改match_parent身高到&#34; wrap_content&#34;这应该可以解决问题:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/buttonDel"
    android:fillViewport="true"
    >

...

答案 1 :(得分:1)

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none" >

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

然而,构建必需结构的正确方法是设计gridview或listview。 使用单个子项滚动视图可以正常运行。