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

前端 未结 7 2014
孤独总比滥情好
孤独总比滥情好 2021-02-20 00:02

I was recommended using a parent view to get horizontal scrolling right in my TextView:



        
相关标签:
7条回答
  • 2021-02-20 00:09

    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

    0 讨论(0)
  • 2021-02-20 00:11

    I'm quite late at this, but here's a simple and easy approach by using rotation of the views.

    horizontal_layout_id.setRotationY(180f)
    

    The above code inverts the whole horizontal layout horizontally, this make the text start on the right instead of left. HOWEVER, this also inverts the text in the textview inside, which is obviously not good.

    textview_inside_horizontal_layout_id.setRotationY(180f)
    

    So, we invert the textview itself once more, this will mirror the mirrored text again! So, you'll end up having something like this-

    This will make the text overflow to the right instead of left as well!

    0 讨论(0)
  • 2021-02-20 00:12

    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"
    
    0 讨论(0)
  • 2021-02-20 00:15
    <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.

    0 讨论(0)
  • 2021-02-20 00:19

    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??

    0 讨论(0)
  • 2021-02-20 00:22

    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);
    
    0 讨论(0)
提交回复
热议问题