Android 运行时布局教程 [英] Android Runtime Layout Tutorial
本文介绍了Android 运行时布局教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道如何在 android 中执行运行时进行 Activity 布局或有很好的参考吗?
Does anyone know how to perform or have a good reference for doing an activity layout at runtime in android?
这是我的活动代码.我确定我只是忽略了在这里做点什么:
Here is the code for my activity. I'm sure I'm just neglecting to do something here:
package com.isi.sa;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SimpleAssessmentTest extends Activity {
LinearLayout layout;
TextView question;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
layout = new LinearLayout(this);
question = new TextView(this);
layout.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
layout.setBackgroundColor(R.color.blue);
question.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
question.setTextColor(R.color.green);
question.setTextSize(1,14);
question.setText("This is question1");
layout.addView(question);
setContentView(layout);
}
}
如您所见,我只是尝试添加带有单个文本视图的线性布局(仅用于测试目的),但是,当 Activity 开始时,我只会看到一个带有应用名称标题栏的黑屏.
As you can see I'm just trying to add a linear layout with a single text view (just for testing purposes) however, when the activity starts I just get a black screen with a title bar of my app name.
谢谢
推荐答案
您忘记设置 contentView.你应该添加
You forgot to set your contentView. You should add
setContentView(layout);
在onCreate
方法的末尾
这篇关于Android 运行时布局教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文