【发布时间】:2014-03-13 09:25:41
【问题描述】:
我创建了一个示例项目并在 Eclipse 中运行“Hello Android Application”。
我了解到可以通过两种方式创建 Textview,使用 XML 标记或使用 Java 代码。
默认情况下,我的示例项目中有一个 Textview 说“Hello world”。我想使用 Java 代码创建一个 Textview 并在上面显示一些消息。
我查了很多,但无法理解代码中提到的步骤和布局设置。
这就是我所做的:
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
TextView tx= new TextView(this);
// tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx.setText("ANDROID APP");
lay
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
另外我不知道如何在addView() 中添加这个文本视图。
这是我的activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
一步一步的解决方案对我很有帮助,任何好的教程链接都会很有价值。提前谢谢!
【问题讨论】:
-
@SRW-782 请选择正确答案,以便将其标记为已回答。
-
获取一步一步的过程,here
标签: java android eclipse textview