2012-11-12 41 views
0

我正在寫一個計算器,它有一個textview和許多按鈕。如果我按下某個按鈕,textview將在字符串末尾添加一個新字符。並自動滾動到正確的方向這個字符。它的工作原理,但老字符(左)正在切割,如果我滾動到右側,我會看到一個空的空間。Horizo​​ntalScroll更新時TextView

<HorizontalScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:fillViewport="true" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_gravity="right" 
      android:gravity="center_vertical|left" 
      android:maxLines="1" 
      android:padding="10dp" 
      android:singleLine="true" 
      android:text="" 
      android:textSize="50sp" /> 
    </HorizontalScrollView> 

看看這個。我輸入123456789123456789。從1到7的數字不可見。正確位置的空格恰好爲7個數字。

It is the left available position when scrollbar

It is the right available position when scrollbar

我讀了很多答案,但所有這不會爲我工作。

回答

1

我不確切知道爲什麼會發生這種情況,但我可以推薦一種方法來避免它。從TextView取出layout_gravity並添加android:scrollbars="none"HorizontalScrollView

<HorizontalScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    android:scrollbars="none"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent"    
     android:gravity="center_vertical|left" 
     android:maxLines="1" 
     android:padding="10dp" 
     android:singleLine="true" 
     android:text="" 
     android:textSize="50sp" /> 
</HorizontalScrollView> 

然後在代碼中,當你修改了TextView的文本添加下面的代碼爲「移動」 HorizontalScrollView到合適的位置:

mHorizontalScroll.post(new Runnable() { 

     @Override 
    public void run() {    
     mHorizontalScroll.scrollTo(mScroll.getWidth(), 0); 
    } 

}); 
+0

非常感謝。但是我在許多代碼中改變了我的textView。它是否存在一種查看方式,而textView將會改變?(也許任何onchangelistener)我想讓我的代碼乾淨。 – pavelartlover

+0

@pavelartlover沒有沒有。但是你總是可以在你的活動中創建一個方法來設置文本併發布'Runnable',這樣當用戶輸入一些數字時你只需調用該方法即可。 – Luksprog