我将文本从一个活动传递到另一个活动
public void letter(View view)
{
Intent intent =new Intent(this,LeaveActivity.class);
EditText editText = (EditText) findViewById(R.id.editText1);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
第二项活动
public class LeaveActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave);
Intent intent=getIntent();
String message = intent.getStringExtra(Le.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
它始终显示左上角的文本,如何在第二个活动中设置其位置,我希望第一个活动中给出的文本在第二个活动中所需的位置,请帮助
答案 0 :(得分:1)
您必须为TextView设置LayoutParams。
textView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setGravity(GRAVITY.CENTER);
更新:
public class LeaveActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=getIntent();
String message = intent.getStringExtra(Le.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setGravity(GRAVITY.CENTER);
textView.setTextSize(40);
textView.setTextColor(COLOR.RED);
textView.setText(message);
setContentView(textView);
}