使 RelativeLayout 可滚动
Make RelativeLayout scrollable
我想制作一个可滚动的 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"
将 ScrollView
高度从“match_parent
”更改为“wrap_content
”这应该可以解决问题:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonDel"
android:fillViewport="true"
>
...
<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 。
为滚动视图使用单个子项以正确运行。
我想制作一个可滚动的 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"
将 ScrollView
高度从“match_parent
”更改为“wrap_content
”这应该可以解决问题:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonDel"
android:fillViewport="true"
>
...
<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 。 为滚动视图使用单个子项以正确运行。