我在一个活动中有一个tablelayout,它包含标题(列名),中间布局(列数据)和页脚(按钮)。现在我需要一个用于中间布局的垂直滚动和用于标题和中间布局的水平滚动。
中间布局的垂直滚动效果很好但水平滚动不起作用。
如何在不使用xml的情况下以编程方式在单个活动中添加双向滚动?
到目前为止,这是我的代码。
RelativeLayout rlBody = new RelativeLayout(this);
rlBody.setPadding(0, 0, 0, 0);
RelativeLayout.LayoutParams bodyParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
rlBody.setLayoutParams(bodyParams);
but_downl = new Button(getApplicationContext());
but_downl.setText("Download");
but_downl.setWidth(width);
RelativeLayout footer = new RelativeLayout(this);
RelativeLayout.LayoutParams footerParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
footerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
footer.setLayoutParams(footerParams);
footer.addView(but_downl);
RelativeLayout rlmaintableLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlmaintableLayoutParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
rlmaintableLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlmaintableLayoutParams.addRule(RelativeLayout.ABOVE, footer.getId());
rlmaintableLayout.setLayoutParams(rlmaintableLayoutParams);
TableLayout maintableLayout = new TableLayout(getApplicationContext());
maintableLayout.setVerticalScrollBarEnabled(true);
TextView tvQnr, tvDownLType, tvPublishBy, tvPublishDate, tvMonPlan, tvLang, textViewMsg;
CheckBox chk, chkparent;
View v;
tableRow = new TableRow(getApplicationContext());
v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
chkparent=new CheckBox(this);
tableRow.addView(chkparent);
tvQnr=new TextView(getApplicationContext());
tvQnr.setText("Name");
tvQnr.setTextColor(Color.parseColor("#235C8B"));
tvQnr.setTypeface(null, Typeface.BOLD);
tvQnr.setTextSize(20);
tvQnr.setPadding(20, 20, 30, 20);
tableRow.addView(tvQnr);
tvDownLType=new TextView(getApplicationContext());
tvDownLType.setText("Age");
tvDownLType.setTextColor(Color.parseColor("#235C8B"));
tvDownLType.setTypeface(null, Typeface.BOLD);
tvDownLType.setTextSize(20);
tvDownLType.setPadding(20, 20, 20, 20);
tableRow.addView(tvDownLType);
tvPublishBy=new TextView(getApplicationContext());
tvPublishBy.setText("Gender");
tvPublishBy.setTextColor(Color.parseColor("#235C8B"));
tvPublishBy.setTypeface(null, Typeface.BOLD);
tvPublishBy.setTextSize(20);
tvPublishBy.setPadding(20, 20, 20, 20);
tableRow.addView(tvPublishBy);
tvPublishDate=new TextView(getApplicationContext());
tvPublishDate.setText("Published Date");
tvPublishDate.setTextColor(Color.parseColor("#235C8B"));
tvPublishDate.setTypeface(null, Typeface.BOLD);
tvPublishDate.setTextSize(20);
tvPublishDate.setPadding(20, 20, 20, 20);
tableRow.addView(tvPublishDate);
tvMonPlan=new TextView(getApplicationContext());
tvMonPlan.setText("Roll No");
tvMonPlan.setTextColor(Color.parseColor("#235C8B"));
tvMonPlan.setTypeface(null, Typeface.BOLD);
tvMonPlan.setTextSize(20);
tvMonPlan.setPadding(20, 20, 20, 20);
tableRow.addView(tvMonPlan);
tvLang=new TextView(getApplicationContext());
tvLang.setText("Language");
tvLang.setTextColor(Color.parseColor("#235C8B"));
tvLang.setTypeface(null, Typeface.BOLD);
tvLang.setTextSize(20);
tvLang.setPadding(20, 20, 20, 20);
tableRow.addView(tvLang);
maintableLayout.addView(tableRow);
maintableLayout.addView(v);
RelativeLayout scrollHolder = new RelativeLayout(this);
RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
scrollHolder.setLayoutParams(scrollHolderParams);
ScrollView myScrollView = new ScrollView(this);
myScrollView.setEnabled(true);
HorizontalScrollView horscrollview = new HorizontalScrollView(this);
horscrollview.setEnabled(true);
TableLayout tableLayout = new TableLayout(getApplicationContext());
int cnt = 0;
for (int j = 0; j < myFormresult.size(); j++){
tableRow = new TableRow(getApplicationContext());
v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
cnt = cnt + 1;
chk = new CheckBox(this);
tvQnr = new TextView(getApplicationContext());
tvQnr.setTextColor(Color.parseColor("#235C8B"));
tvQnr.setTextSize(15);
tvQnr.setText(myFormresult.get(j).getFormName());
tvQnr.setPadding(25, 10, 20, 10);
tvDownLType = new TextView(getApplicationContext());
tvDownLType.setTextColor(Color.parseColor("#235C8B"));
tvDownLType.setTextSize(15);
tvDownLType.setText(myFormresult.get(j).getVersion());
tvDownLType.setPadding(25, 10, 20, 10);
tvPublishBy = new TextView(getApplicationContext());
tvPublishBy.setTextColor(Color.parseColor("#235C8B"));
tvPublishBy.setTextSize(15);
tvPublishBy.setText(myFormresult.get(j).getPublishedBy());
tvPublishBy.setPadding(25, 10, 20, 10);
tvPublishDate = new TextView(getApplicationContext());
tvPublishDate.setTextColor(Color.parseColor("#235C8B"));
tvPublishDate.setTextSize(15);
tvPublishDate.setText(myFormresult.get(j).getPublishedDate());
tvPublishDate.setPadding(25, 10, 20, 10);
tvMonPlan = new TextView(getApplicationContext());
tvMonPlan.setTextColor(Color.parseColor("#235C8B"));
tvMonPlan.setTextSize(15);
tvMonPlan.setText(myFormresult.get(j).getMonitoringPlan());
tvMonPlan.setPadding(25, 10, 20, 10);
tvLang = new TextView(getApplicationContext());
tvLang.setTextColor(Color.parseColor("#235C8B"));
tvLang.setTextSize(15);
tvLang.setText(myFormresult.get(j).getLanguage());
tvLang.setPadding(25, 10, 20, 10);
tableRow.addView(chk);
tableRow.addView(tvQnr);
tableRow.addView(tvDownLType);
tableRow.addView(tvPublishBy);
tableRow.addView(tvPublishDate);
tableRow.addView(tvMonPlan);
tableRow.addView(tvLang);
tableLayout.addView(tableRow);
tableLayout.addView(v);
tableLayout.addView(vertView);
}
if (cnt == 0) {
textViewMsg = new TextView(getApplicationContext());
textViewMsg.setText("No records found ...");
textViewMsg.setTextColor(Color.parseColor("#235C8B"));
textViewMsg.setTextSize(15);
tableLayout.addView(textViewMsg);
but_downl.setEnabled(false);
}
myScrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
horscrollview.addView(tableLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
myScrollView.addView(horscrollview);
scrollHolder.addView(myScrollView);
maintableLayout.addView(scrollHolder);
rlmaintableLayout.addView(maintableLayout);
rlBody.addView(rlmaintableLayout);
rlBody.addView(footer);
提前致谢...
答案 0 :(得分:0)
我创建了具有水平和垂直滚动的表格布局。标题也固定在垂直滚动。
我发送了类和xml文件的代码,您可以根据需要自定义它。 我希望它会对你有所帮助。
创建MainActivity类:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
HorizontalScrollView hori,hori2;
int viewWidth;
TableLayout scrollablePart;
GestureDetector gestureDetector = null;
ArrayList<LinearLayout> layouts;
LinearLayout linear;
private int scrollMax;
int currPosition, prevPosition;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableRow.LayoutParams wrapWrapTableRowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int[] fixedColumnWidths = new int[]{20, 20, 20, 20, 20, 20};
int[] scrollableColumnWidths = new int[]{20,20, 20, 20, 20, 20};
int fixedRowHeight = 50;
int fixedHeaderHeight = 60;
TableRow row = new TableRow(this);
//header (fixed horizontally)
TableLayout fixedColumn = (TableLayout) findViewById(R.id.fixed_column);
TableLayout fixedColumn2 = (TableLayout) findViewById(R.id.fixed_header_column);
TextView fixedView2 = makeTableRowWithText("col 1 ", scrollableColumnWidths[0], fixedRowHeight);
fixedView2.setBackgroundColor(Color.LTGRAY);
fixedColumn2.addView(fixedView2);
//rest of the table (within a scroll view)
scrollablePart = (TableLayout) findViewById(R.id.scrollable_part);
//header (fixed vertically)
hori=(HorizontalScrollView)findViewById(R.id.horizon);
hori.setVerticalScrollBarEnabled(false);
hori.setHorizontalScrollBarEnabled(false);
hori2=(HorizontalScrollView)findViewById(R.id.hr_2);
//hori.onScrollChanged
hori2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
moveScrollView();
cal();
//startAutoScrolling();
return false;
}
});
ScrollView scrol=(ScrollView)findViewById(R.id.Scroll);
//scrol.onScrollChanged(fixedHeaderHeight, fixedHeaderHeight, fixedHeaderHeight, fixedHeaderHeight);
linear=(LinearLayout)findViewById(R.id.fillable_area);
TableLayout header = (TableLayout) findViewById(R.id.table_header);
//scrollablePart.onGenericMotionEvent(MotionEvent.ACTION_MOVE);
ViewTreeObserver vto = linear.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
linear.getViewTreeObserver().removeGlobalOnLayoutListener(this);
getScrollMaxAmount();
}
});
hori2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
moveScrollView();
//startAutoScrolling();
return false;
}
});
row.setLayoutParams(wrapWrapTableRowParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(Color.GRAY);
row.addView(makeTableRowWithText("col 2", fixedColumnWidths[1], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 3", fixedColumnWidths[2], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 4", fixedColumnWidths[3], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 5", fixedColumnWidths[4], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 6", fixedColumnWidths[5], fixedHeaderHeight));
header.addView(row);
for(int i = 0; i < 50; i++)
{
TextView fixedView = makeTableRowWithText("row number " + i, scrollableColumnWidths[0], fixedRowHeight);
fixedView.setBackgroundColor(Color.BLUE);
fixedColumn.addView(fixedView);
row = new TableRow(this);
row.setLayoutParams(wrapWrapTableRowParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(Color.WHITE);
row.addView(makeTableRowWithText("value 2", scrollableColumnWidths[1], fixedRowHeight));
row.addView(makeTableRowWithText("value 3", scrollableColumnWidths[2], fixedRowHeight));
row.addView(makeTableRowWithText("value 4", scrollableColumnWidths[3], fixedRowHeight));
row.addView(makeTableRowWithText("value 5", scrollableColumnWidths[4], fixedRowHeight));
row.addView(makeTableRowWithText("value 6", scrollableColumnWidths[5], fixedRowHeight));
scrollablePart.addView(row);
}
}
private void cal(){
final Handler handler2 = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run()
{
if(i==0)
{
try{
moveScrollView();
}catch (Exception e){
}
i++;
}
handler2.postDelayed(this,500);
}
};
handler2.postDelayed(runnable, 500);
}
public void getScrollMaxAmount(){
int actualWidth = (linear.getMeasuredWidth()-512);
scrollMax = actualWidth;
System.out.println("scrollMax"+scrollMax);
}
private Timer scrollTimer = null;
private Timer clickTimer = null;
private Timer faceTimer = null;
private TimerTask clickSchedule;
private TimerTask scrollerSchedule;
private TimerTask faceAnimationSchedule;
private int scrollPos = 0;
public void startAutoScrolling(){
if (scrollTimer == null) {
scrollTimer = new Timer();
final Runnable Timer_Tick = new Runnable() {
public void run() {
moveScrollView();
}
};
if(scrollerSchedule != null){
scrollerSchedule.cancel();
scrollerSchedule = null;
}
scrollerSchedule = new TimerTask(){
@Override
public void run(){
runOnUiThread(Timer_Tick);
}
};
scrollTimer.schedule(scrollerSchedule, 30, 30);
}
}
public void moveScrollView(){
scrollPos = (int) (hori.getScrollX() + 1.0);
scrollPos = (int) (hori2.getScrollX() + 1.0);
System.out.println("scrollPos"+scrollPos);
if(scrollPos >= scrollMax){
scrollPos = 0;
}
hori2.scrollTo(scrollPos, 0);
hori.scrollTo(scrollPos, 0);
}
private TextView recyclableTextView;
public TextView makeTableRowWithText(String text, int widthInPercentOfScreenWidth, int fixedHeightInPixels) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
recyclableTextView = new TextView(this);
recyclableTextView.setText(text);
recyclableTextView.setTextColor(Color.BLACK);
recyclableTextView.setTextSize(20);
recyclableTextView.setWidth(widthInPercentOfScreenWidth * screenWidth / 100);
recyclableTextView.setHeight(fixedHeightInPixels);
return recyclableTextView;
}
}
创建activity_main.xmml,如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:id="@+id/fillable_area">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:gravity="center_horizontal"
android:id="@+id/fillable_area1">
<TableLayout
android:id="@+id/fixed_header_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<HorizontalScrollView
android:id="@+id/horizon"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/table_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</HorizontalScrollView>
</LinearLayout>
<ScrollView
android:id="@+id/Scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:id="@+id/fillable_area">
<TableLayout
android:id="@+id/fixed_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<HorizontalScrollView
android:id="@+id/hr_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/scrollable_part"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>