0
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="none">
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:singleLine="true"
    android:text=""
    android:layout_gravity="right"
    android:gravity="right|center_vertical"
    android:textColor="#666666"
    android:textSize="30sp" />
</HorizontalScrollView>

Hi, i want the text inside the textView to be scrollable when it fills all the space available. The text is inserted from the right, so the beginning of the text disappears on the left. When it becames scrollable i can scroll an amount of space equal to the space of the text that's not displayed but it scrolls only to the right side, where textView is empty and i can't reach what I've typed before. Suggestions? Sorry for poor English. Wish it's clear enough... (the scrollView is part of a vertical linearLayout)

4

1 回答 1

0

将 android:layout_gravity="right" 从 TextView 移动到 Horizo​​ntalScrollView 将
TextView 重力更改为仅 "center_vertical"
在 Activity.java 中编写一个方法来设置 TextView 中的文本并向右滚动。
当您想在 TextView 中设置文本时,请使用此方法。

示例代码:

XML

<HorizontalScrollView
    android:id="@+id/scrollView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scrollbars="none"
    android:layout_gravity="right">

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:singleLine="true"
    android:text=""
    android:gravity="center_vertical"
    android:textColor="#666666"
    android:textSize="30sp"/>

</HorizontalScrollView>

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="ClickMe" 
    android:onClick="btClick"/>

</LinearLayout>

活动.java:

    public class MainActivity extends Activity {

    private TextView textView;
    private HorizontalScrollView scroll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scroll = (HorizontalScrollView)findViewById(R.id.scrollView);
        textView = (TextView) findViewById(R.id.textView2);

        SetTextViewText("Inicial Text");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void btClick(View v) {

        String text = textView.getText().toString();
        int rnd = 1 + (int)(Math.random() * ((9 - 1) + 1));//Get a random number between 1 and 9 
        text += rnd; //Append random to previous text
        SetTextViewText(text); //Set TextView with new text;
    }

    private void SetTextViewText(String text)// Set text to TexView and scroll all to the right
    {
        textView.setText(text);
        scroll.post(new Runnable() {
            public void run() {
                scroll.fullScroll(ScrollView.FOCUS_RIGHT);
            }
        });     
    }
}

我希望这是你所要求的。

于 2013-07-08T15:34:42.173 回答