Android运行布局教程 [英] Android Runtime Layout Tutorial
本文介绍了Android运行布局教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道如何执行或对android系统中做一个活动的布局在运行时一个很好的参考?
Does anyone know how to perform or have a good reference for doing an activity layout at runtime in android?
下面是$ C $下我的活动。我敢肯定,我只是忽视做的东西在这里:
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);
}
}
正如你所看到的,我只是想添加一个线性布局,一个文本视图(只是为了测试目的),然而,在活动开始时,我只是得到一个黑色的屏幕,我的应用程序名称的标题栏。
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.
感谢
推荐答案
您忘了把内容查看。您应该添加
You forgot to set your contentView. You should add
setContentView(layout);
在年底的的onCreate
方法
这篇关于Android运行布局教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文