Android, make scrollable view overflow to the left instead of right?

匿名 (未验证) 提交于 2019-12-03 03:05:02

问题:

I was recommended using a parent view to get horizontal scrolling right in my 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> 

Horizontal scrolling works fine but it makes the content overflow to the right when it gets longer than it's parent's width:

------- |123456|7 ------- 

However I'm working on a textview that holds numbers and since numbers are commonly aligned to the right I need the textview to overflow to the opposite side. (you should have to scroll left to see the beginning of the string).

 ------ 1|4567|  ------ 

I have tried multiple combinations of gravity="right" and widths but I cannot manage to do it. How can I align the text to the right and make it overflow to the left?


EDIT:

I tried doing this when the user types:

calc.setText( newNumber ); HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv); hsv.scrollTo(hsv.getRight(), hsv.getTop()); 

This scrolls to the right every time the user types a number, however the latest number is always left out of the screen (it's like it scrolled and THEN added the number).

回答1:

I have tested the code . So there are two ways to get around this problem First one with ScrollBars

new Handler().postDelayed(new Runnable(){    @Override public void run() {     HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv1);         hsv.scrollTo(hsv.getRight(), hsv.getTop());     }         },100L); 

Second one is without scrollBars

 <TextView   android:ellipsize="start"   //add this line  ...  /> 


回答2:

Inspired by this

You can make your own class derived from 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);     } } 

I posted a complete simple project there



回答3:

<HorizontalScrollView      android:id="@+id/hsv1"     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>     

Added id to the HorizontalScrollView

HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.hsv1); hsv.scrollTo(hsv.getRight(), hsv.getTop()); 

This is untested as I made it on the fly. Tell me how it goes.



回答4:

I think you must use android:fillViewport="true" property for scroll-view
Check This & This

Go through a Tutorial

   <ScrollView          android:fillViewport="true"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:id="@+id/scrollView"/> 

Doubt: Where is your container layout for scroll-view??



回答5:

Did you try assigning the ellipsize parameter to your textview?

android:singleLine="true" android:ellipsize="start" 

I hope this will solve your issue.

For setting the gravity of the text when not overflowing, try setting

android:gravity="center_vertical|right" 


回答6:

try this , this would work properly and will not give you any delays when you jump between threads

hor = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);     hor.postDelayed(new Runnable() {         public void run() {             hor.fullScroll(HorizontalScrollView.FOCUS_RIGHT);         }     }, 1L); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!