make a TextView scrollable + Bug in Android
I have already read this post: Making TextView scrollable on Android without success.
My app looks like this:
Where the black space is a TextView. It is declared at the xml like this:
<TextView
android:id="@+id/consola"
android:layout_width="320px"
android:layout_height="333px"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:scrollbars = "vertical"
android:gravity="top|left"
android:inputType="textMultiLine"
>
</TextView>
And my code takes text into the editText when a button is pressed, and writes a new line at the textView with that text. Code looks like:
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView miConsola = (Text开发者_开发技巧View) findViewById(R.id.consola);
miConsola.setMovementMethod(new ScrollingMovementMethod());
final EditText lineaComando = (EditText) findViewById(R.id.linea_comando);
final Button botonConectar = (Button) findViewById(R.id.boton_conectar);
final Button botonEnviar = (Button) findViewById(R.id.boton_enviar);
botonEnviar.setEnabled(false);
botonConectar.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Intentaremos conectar por bluetooth aqui
botonConectar.setEnabled(false);
botonEnviar.setEnabled(true);
}
});
botonEnviar.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Enviamos el comando
CharSequence comando = lineaComando.getText();
miConsola.append(comando+"\r\n");
}
});
miConsola.append("Esto es una prueba\r\n");
miConsola.append("Esto es otra prueba\r\n");
}
}
But when the text reaches the bottom of the TextView, it still writes a new line over the EditText, and if i go on, no scroll bar appear.
Any idea of what I'm doing wrong?
put your text view in a vertical ScrollView.Set the height of the Scroll View some fixed height as you did for your text view.Then set wrap_content to your text view's width and height.
Using hard-coded pixel values in your xml layout is not the best way. Use a relative layout and set the TextView to be above your EditText. I put my TextViews in ScrollViews also (using the relative layout above attribute to keep them in place).
精彩评论