Android 1 个父 ScrollView,3 个垂直滚动的 TextView
Android 1 Parent ScrollView, 3 TextView with vertical scrolling
我对 android 编程有疑问。
我有一个整个 activity 的父滚动视图,并且有三个具有滚动功能的文本视图。但是,当我使用以下代码时,它似乎根本不起作用。只有父滚动可用。
final View view = inflater.inflate(R.layout.activity_register_terms_fragment, container, false);
TextView basicTermView = (TextView) view.findViewById(R.id.register_terms_basic_info);
TextView purposeTermView = (TextView) view.findViewById(R.id.register_terms_purpose_info);
TextView provideTermView = (TextView) view.findViewById(R.id.register_terms_provide_info);
TextView previous = (TextView) view.findViewById(R.id.register_terms_pre);
TextView next = (TextView) view.findViewById(R.id.register_terms_next);
basicTermView.setMovementMethod(new ScrollingMovementMethod());
purposeTermView.setMovementMethod(new ScrollingMovementMethod());
provideTermView.setMovementMethod(new ScrollingMovementMethod());
我应该如何更改代码?
谢谢你帮助我!
您不能在 ScrollView
中有可滚动的视图,例如 TextView
或 ListView
或 RecyclerView
。因此,在普通布局中使用简单的 TextView 并向其添加 android:scrollbars 属性 或者您可以使用视图的自定义 class ,它将以编程方式计算视图的 width/height 并使用 ScrollView
因为它是父级。
例如,要在scrollview中使用Listview
,我们需要使用下面自定义的class of Listview
,它会计算列表项的高度并进行设置。
public class ExpandedListView extends ListView {
private ViewGroup.LayoutParams params;
private int old_count = 0;
public ExpandedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandedListView(Context context) {
super(context);
}
public ExpandedListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
@Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
this.setScrollContainer(false);
old_count = getCount();
params = getLayoutParams();
params.height = getCount()
* (old_count > 0 ? getChildAt(0).getHeight() : 0);
setLayoutParams(params);
}
super.onDraw(canvas);
}
}
我对 android 编程有疑问。
我有一个整个 activity 的父滚动视图,并且有三个具有滚动功能的文本视图。但是,当我使用以下代码时,它似乎根本不起作用。只有父滚动可用。
final View view = inflater.inflate(R.layout.activity_register_terms_fragment, container, false);
TextView basicTermView = (TextView) view.findViewById(R.id.register_terms_basic_info);
TextView purposeTermView = (TextView) view.findViewById(R.id.register_terms_purpose_info);
TextView provideTermView = (TextView) view.findViewById(R.id.register_terms_provide_info);
TextView previous = (TextView) view.findViewById(R.id.register_terms_pre);
TextView next = (TextView) view.findViewById(R.id.register_terms_next);
basicTermView.setMovementMethod(new ScrollingMovementMethod());
purposeTermView.setMovementMethod(new ScrollingMovementMethod());
provideTermView.setMovementMethod(new ScrollingMovementMethod());
我应该如何更改代码? 谢谢你帮助我!
您不能在 ScrollView
中有可滚动的视图,例如 TextView
或 ListView
或 RecyclerView
。因此,在普通布局中使用简单的 TextView 并向其添加 android:scrollbars 属性 或者您可以使用视图的自定义 class ,它将以编程方式计算视图的 width/height 并使用 ScrollView
因为它是父级。
例如,要在scrollview中使用Listview
,我们需要使用下面自定义的class of Listview
,它会计算列表项的高度并进行设置。
public class ExpandedListView extends ListView {
private ViewGroup.LayoutParams params;
private int old_count = 0;
public ExpandedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandedListView(Context context) {
super(context);
}
public ExpandedListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
@Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
this.setScrollContainer(false);
old_count = getCount();
params = getLayoutParams();
params.height = getCount()
* (old_count > 0 ? getChildAt(0).getHeight() : 0);
setLayoutParams(params);
}
super.onDraw(canvas);
}
}