【问题标题】:Can't get .getwidth() and .getheight() to work无法让 .getwidth() 和 .getheight() 工作
【发布时间】:2015-05-02 00:44:15
【问题描述】:

我知道关于这个主题还有很多其他问题,但我已经浏览了所有这些问题,但仍然没有解决问题。

我制作了这段代码用于测试:

public class MainActivity extends Activity {

RelativeLayout layout;
TextView widthtext;
TextView heighttext;
int width;
int height;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout = (RelativeLayout) findViewById(R.id.rela);

    widthtext = (TextView) findViewById(R.id.textView);
    heighttext = (TextView) findViewById(R.id.textView2);

    layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            width = layout.getWidth();
            height = layout.getHeight();
        }
    });

    widthtext.setText(width);
    heighttext.setText(height);
}

<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/rela">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="123"
    android:id="@+id/textView"
    android:layout_alignTop="@+id/textView2"
    android:layout_toLeftOf="@+id/textView2"
    android:layout_toStartOf="@+id/textView2"
    android:layout_marginRight="50dp"
    android:layout_marginEnd="50dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="123"
    android:id="@+id/textView2"
    android:layout_marginRight="50dp"
    android:layout_marginEnd="50dp"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

经过一番麻烦后,我现在注意到宽度和高度 int 具有正确的值,但我无法让它们显示在我的 TextViews 中,获得 NullPointerExeption。

谁能做到这一点?

额外:

public class MainActivity extends Activity {

RelativeLayout layout;
TextView widthtext;
TextView heighttext;
int width;
int height;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout = (RelativeLayout) findViewById(R.id.rela);

    widthtext = (TextView) findViewById(R.id.textView);
    heighttext = (TextView) findViewById(R.id.textView2);

    layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            width = layout.getWidth();
            height = layout.getHeight();
        }
    });

    widthtext.setText(width + "");
    heighttext.setText(height + "");
}

}

【问题讨论】:

  • 你确定它是 NPE 吗?尝试在将 int 传递给 setText 之前将其转换为字符串。例如widthtext.setText("""+width);heighttext.setText(""+height);

标签: java android height width


【解决方案1】:

你正在这样做

widthtext.setText(width);
heighttext.setText(height);

在你的听众之外,所以自然它还没有值。

把它们移到里面

layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        width = layout.getWidth();
        height = layout.getHeight();

        // move here
        widthtext.setText(width);
        heighttext.setText(height);
    }
});
}

正如blackbeltcomment 中指出的那样,您正在调用setText() 并传递int。这将查找该值的资源 id。将它们更改为Strings

widthtext.setText("" + width);
heighttext.setText("" + height);

【讨论】:

  • orb,请注意。整数宽度,整数高度
  • 谢谢。 bor @Blackbelt 将修复
  • 这是我首先尝试的,但随后应用程序崩溃了。还在做这个。
  • 至少它不会崩溃:D
  • put widthtext.setText(width + ""); heighttext.setText(height + "");检索宽度和高度后在 onGlobalLayout 中
【解决方案2】:

setText 取字符串值

所以而不是

setText(width);
setText(height);

setText(width+"");
setText(height+"");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-30
    • 2011-09-02
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2012-01-10
    相关资源
    最近更新 更多