gpt4 book ai didi

android - 垂直和水平 ScrollView

转载 作者:行者123 更新时间:2023-11-29 00:25:02 25 4
gpt4 key购买 nike

我正在使用需要垂直和水平滚动的表格。我使用了两个 ScrollView ,如下所示。是否可以将一个用于两个目的?意思是启用一个 ScrollView 作为水平和垂直:

LinearLayout contentView = (LinearLayout) findViewById(R.id.contentView);
TableLayout tableLayout = new TableLayout(getApplicationContext());
TableRow tableRow;
TextView textView;

for (int i = 0; i <28; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < 16; j++) {
textView = new TextView(getApplicationContext());
textView.setText("test");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
ScrollView scroll = new ScrollView(MainActivity.this);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(tableLayout);
HorizontalScrollView horizontalScroll = new HorizontalScrollView(MainActivity.this);
horizontalScroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
horizontalScroll.addView(scroll);
contentView.addView(horizontalScroll);

最佳答案

您可以使用可在 xml 文件中使用的自定义 ScrollView 代码。这是垂直 ScrollView 的一个小示例,您也可以使用水平 ScrollView

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class VerticalScrollview extends ScrollView {

public VerticalScrollview(Context context) {
super(context);
}

public VerticalScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
}

public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.i("VerticalScrollview",
"onInterceptTouchEvent: DOWN super false");
super.onTouchEvent(ev);
break;

case MotionEvent.ACTION_MOVE:
return false; // redirect MotionEvents to ourself

case MotionEvent.ACTION_CANCEL:
Log.i("VerticalScrollview",
"onInterceptTouchEvent: CANCEL super false");
super.onTouchEvent(ev);
break;

case MotionEvent.ACTION_UP:
Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false");
return false;

default:
Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action);
break;
}

return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction());
return true;
}
}

关于android - 垂直和水平 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20394286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com