I have a dynamic table that can have as few as 2 rows but can also have upto 50 rows. I am trying to wrap that table within a scrollview. I also need to have 2 buttons that should show up at the bottom of the page.
Here is what I am trying to achieve but am getting stuck (attached the images to show what I am trying):
Can someone suggest what I am doing wrong ? I scanned through a lot of questions on StackOverflow but couldnt find something that addresses this particular situation.
Below is my layout file and I populate the table in the activity :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/mainTable">
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="425dp"
android:layout_gravity="center_horizontal"
android:text="Submit" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_horizontal"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:2)
我会考虑在RecyclerView
中使用TableLayout
而不是ScrollView
。如果可以为按钮视图布局设置静态高度,则可以操纵填充和边距以获得所需的效果。请参见下面根据此答案修改的:https://stackoverflow.com/a/45161728/6723646
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/sv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="45dp">
<TableLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/mainTable">
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_marginTop="-45dp"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Submit" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_horizontal"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
答案 1 :(得分:0)
感谢@airfish和@Onik为您提供解决方案。在过去两天的苦苦挣扎之后,@ airfish的解决方案最终帮助解决了我的问题。
这是最终的解决方案,对于任何有兴趣使此功能起作用的人。
最终版式文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="45dp">
<TableLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal"
android:id="@+id/mainTable">
</TableLayout>
</ScrollView>
<LinearLayout
android:layout_marginTop="-45dp"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Submit" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_horizontal"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
MainActivity.java(编辑第30行以增加/减少要测试的行数)
package com.example.scroll_ex;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout mainTable = findViewById(R.id.mainTable);
mainTable.removeAllViews();
int leftRowMargin = 0;
int topRowMargin = 0;
int rightRowMargin = 0;
int bottomRowMargin = 0;
int textSize = 0, smallTextSize = 0, mediumTextSize = 0;
TableRow tr = new TableRow(this);
TextView column1 = new TextView(this);
TextView column2 = new TextView(this);
for(int i=-1; i < 25; i++){
tr = new TableRow(this);
column1 = new TextView(this);
column2 = new TextView(this);
tr.setGravity(Gravity.CENTER);
tr.setId(i+ 1);
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
tr.setPadding(0,0,0,0);
tr.setLayoutParams(trParams);
if (i == -1) {
column1.setText("Name");
column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
column1.setPadding(5, 5, 1, 5);
} else {
column1.setText("Name #" + i);
column1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
column1.setPadding(5, 0, 1, 5);
}
tr.addView(column1);
if (i == -1) {
column2.setText("Address");
column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
column2.setPadding(5, 5, 1, 5);
} else {
column2.setText("Address " + i);
column2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
column2.setPadding(5, 0, 1, 5);
}
tr.addView(column2);
mainTable.addView(tr, trParams);
}
}
}