我建议使用父视图在我的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>
Run Code Online (Sandbox Code Playgroud)
水平滚动工作正常,但当内容长于父的宽度时,内容会向右溢出:
-------
|123456|7
-------
Run Code Online (Sandbox Code Playgroud)
但是我正在研究一个包含数字的textview,因为数字通常与右边对齐,我需要将textview溢出到另一边.(你必须向左滚动才能看到字符串的开头).
------
1|4567|
------
Run Code Online (Sandbox Code Playgroud)
我尝试过多次重力="右"和宽度的组合,但我无法做到.如何将文本右对齐并使其向左溢出?
编辑:
当用户键入时,我尝试这样做:
calc.setText( newNumber );
HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv);
hsv.scrollTo(hsv.getRight(), hsv.getTop());
Run Code Online (Sandbox Code Playgroud)
每次用户输入一个数字时,它会向右滚动,但是最新的数字总是被排除在屏幕之外(就像它滚动一样,然后添加了数字).
受此启发
您可以从 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);
}
}
Run Code Online (Sandbox Code Playgroud)
我在那里发布了一个完整的简单项目
我已经测试了代码。所以有两种方法可以解决这个问题第一个方法是使用ScrollBars
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv1);
hsv.scrollTo(hsv.getRight(), hsv.getTop());
}
},100L);
Run Code Online (Sandbox Code Playgroud)
第二个是没有滚动条的
<TextView
android:ellipsize="start" //add this line
...
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4871 次 |
| 最近记录: |