有人建议我使用父 View 在我的 TextView 中获得水平滚动:
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scrollHorizontally="true"
android:gravity="center|right"
android:text="123456789"/>
</HorizontalScrollView>
水平滚动效果很好,但当内容超出其父级的宽度时,它会使内容溢出到右侧:
-------
|123456|7
-------
但是我正在处理包含数字的 TextView ,并且由于数字通常向右对齐,所以我需要 TextView 溢出到另一侧。 (您必须向左滚动才能看到字符串的开头)。
------
1|4567|
------
我尝试了多种 gravity="right"和 widths 的组合,但我无法做到。如何使文本向右对齐并使其向左溢出?
编辑:
我尝试在用户输入时这样做:
calc.setText( newNumber );
HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv);
hsv.scrollTo(hsv.getRight(), hsv.getTop());
每次用户输入数字时,它都会向右滚动,但是最新的数字总是留在屏幕之外(就像滚动然后添加数字一样)。
最佳答案
灵感来自 this
您可以从 HorizontalScrollView 派生自己的类
public class RightAlignedHorizontalScrollView extends HorizontalScrollView {
public RightAlignedHorizontalScrollView(Context context) {
super(context);
}
public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RightAlignedHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
scrollTo(getChildAt(0).getMeasuredWidth(), 0);
}
}
我发布了一个完整的简单项目 there
关于java - Android,使可 ScrollView 溢出到左侧而不是右侧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14105417/