本来是想做一个显示文字信息的,当文字很多时View的高度不能超过一个固定的值,当文字很少时View的高度小于那个固定值时,按View的高度显示。因为ScrollView没有maxHeight,无法满足需求,只好另找方法了。
View本身是可以设置ScrollBar,这样就不一定需要依赖ScrollView了。TextView有个属性maxLine,这样也就满足了需求了,只要设置一个TextView带ScrollBar的,然后设置maxLine就可以了。
- <TextView
- android:id="@+id/text_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:singleLine="false"
- android:maxLines="10"
- android:scrollbars="vertical"
- />
<TextView android:id="@+id/text_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="false" android:maxLines="10" android:scrollbars="vertical" />
还需要在代码了设置TextView可以滚动。
- TextView textView = (TextView)findViewById(R.id.text_view);
- textView.setMovementMethod(ScrollingMovementMethod.getInstance());
------------------------------------------------------------------------------------------------------------------------------------------
如果用ScrollView ,代码如下,<ScrollView
android:layout_width="745dip"
android:layout_height="520dip"
android:id="@+id/mBtnRe2"
android:layout_marginTop="30dip"
android:layout_marginLeft="30dip"
android:fadingEdge="none"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mTxtContent"
android:textSize="25sp"
android:textColor="#000000"
android:fadingEdge="none"
/>
</ScrollView>
--------------------------------------------------------------------------------------------------------------------------------------------
ScrollView可以调整滑动速度,自己实现ScrollView
**
- * 快/慢滑动ScrollView
- * @author 农民伯伯
- *
- */
- public class SlowScrollView extends ScrollView {
- public SlowScrollView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public SlowScrollView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public SlowScrollView(Context context) {
- super(context);
- }
- /**
- * 滑动事件
- */
- @Override
- public void fling(int velocityY) {
- super.fling(velocityY / 4);
- }
- }
- 复制代码代码说明: