oncreate函数中的布局宽度,我需要这个大小,但你的英勇值是0.这里有一些我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout_principal=(LinearLayout)findViewById(R.id.pru);
int mywidth=layout_principal.getWidth();
LinearLayout.LayoutParams txt_sep_principal_Params=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
mywidth/2);
TextView txt_sep_principal=new TextView(this);
txt_sep_principal.setLayoutParams(txt_sep_principal_Params);
txt_sep_principal.setText("text of my code");
TextView txt_sep_principal2=new TextView(this);
txt_sep_principal2.setLayoutParams(txt_sep_principal_Params);
txt_sep_principal2.setText("Text of my code");
layout_principal.addView(txt_sep_principal);
layout_principal.addView(txt_sep_principal2);
}
答案 0 :(得分:1)
此行之后
LinearLayout layout_principal=(LinearLayout)findViewById(R.id.pru);
尝试添加这些代码行。
layout_principal.setOrientation(LinearLayout.VERTICAL);
layout_principal.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
答案 1 :(得分:0)
layout
可能尚未完全完成绘图。尝试在其上发布runnable
。像
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout_principal=(LinearLayout)findViewById(R.id.pru);
layout_principal.post(new Runnable()
{
public void run()
{
int mywidth=layout_principal.getWidth();
LinearLayout.LayoutParams txt_sep_principal_Params=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
mywidth/2);
TextView txt_sep_principal=new TextView(this);
...
}
});
修改强>
public class YourActivty extends Activity
{
// These are declarations
TextView tv;
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// these are initializations
tv = (TextView) findViewById(R.id.someId);
btn = (Button) findViewById(R.id.someOtherId);
请参阅我的代码中的评论。 以这种方式声明使其可供整个 Activity
使用。在调用setContentView()
之前,您无法初始化它们,当然也不能在方法之外。显然,您需要替换我自己使用的样本View
。
答案 2 :(得分:0)
在oncreate方法中获取布局大小还为时过早
试试这段代码:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
//Here you can get the size!
Toast.makeText(this,""+ Layout.getHeight(), Toast.LENGTH_LONG).show();
}
在这里查看完整代码:
LinearLayout layout_principal;
int mywidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout_principal =(LinearLayout)findViewById(R.id.pru);
LinearLayout.LayoutParams txt_sep_principal_Params=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
mywidth/2);
TextView txt_sep_principal=new TextView(this);
txt_sep_principal.setLayoutParams(txt_sep_principal_Params);
txt_sep_principal.setText("text of my code");
TextView txt_sep_principal2=new TextView(this);
txt_sep_principal2.setLayoutParams(txt_sep_principal_Params);
txt_sep_principal2.setText("Text of my code");
layout_principal.addView(txt_sep_principal);
layout_principal.addView(txt_sep_principal2);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
//Here you can get the size!
mywidth=layout_principal.getWidth();
}