【问题标题】:Make RelativeLayout scrollable使 RelativeLayout 可滚动
【发布时间】:2016-11-16 15:45:33
【问题描述】:

我想制作一个可滚动的 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>

Java:

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"

【问题讨论】:

  • 尝试只带一个孩子的滚动视图
  • 使用 ArrayAdapter 和 listview 代替
  • 或者一个 RecyclerView 和一个 Grid 布局管理器
  • 将您的相对布局包装在滚动包装器中

标签: java android nested scrollview android-relativelayout


【解决方案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 。 使用单个子项让滚动视图正常运行。

【讨论】:

  • 不走运,和我的结果一样。
  • 尝试包装内容并关闭所有标签,绝对有效@Robert
  • 您的第一个相对布局是否正确指定了坐标,我可以看到您的相对位置是重叠的,请参阅线程stackoverflow.com/questions/6674341/… 了解official doc here
  • 还可以尝试组合包装内容并匹配父项以获得您想要的内容
  • 很好,您可以进一步自定义和美化它,但请记住,相对布局的工作方式与顺序无关,并且像 1,2 etc.cheers 这样一个接一个地线性化
【解决方案2】:

ScrollView 的高度从“match_parent”更改为“wrap_content”,这应该可以解决问题:

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

...

【讨论】:

  • 坦克的答案,但现在只显示第一行
猜你喜欢
  • 2015-12-17
  • 1970-01-01
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
相关资源
最近更新 更多