无法检索整数getHeight()

时间:2015-05-12 16:23:28

标签: java android

我已经以编程方式创建了Layout'布局',然后我在相同的布局中创建了TextView以显示layout height,没有其他方法或课堂上的其他任何东西。不幸的是应用程序关闭了,我无法确定原因。

LinearLayout layout =new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lparams.setMargins(0, 10, 0, 10);
        lparams.gravity= Gravity.CENTER;


        TextView fimText = new TextView(this);
        fimText.setLayoutParams(lparams);
        fimText.setText(layout.getHeight());
        layout.addView(fimText, lparams);
        setContentView(layout);

2 个答案:

答案 0 :(得分:0)

您需要将LinearLayout添加到容器中,然后才能在其上调用getHeight。此外,getHeight返回一个int,因此您需要将其转换为String。

试试这个:

LinearLayout layout =new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,     LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 10, 0, 10);
lparams.gravity= Gravity.CENTER;

setContentView(layout);

TextView fimText = new TextView(this);
fimText.setLayoutParams(lparams);
fimText.setText(String.valueOf(layout.getHeight()));
layout.addView(fimText, lparams);

答案 1 :(得分:0)

这是因为layout.getHeight()不会返回CharSequence值。添加.toString()""+