移動機器人:layout_gravity =「權利」從TextView中到HorizontalScrollView
變化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>
Activity.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);
}
});
}
}
我希望這是你要求什麼。
是的,它的工作!非常感謝,你真的幫了我。 – user2557804