setWidth()和setHeight()没反应的问题,onCreate()里面获取控件的高度是0

    editText=(EditText)findViewById(R.id.myEditText);
    // editText.setHeight(10); //不生效
    editText.getLayoutParams().height = 100;

onCreate()里面获取控件的高度是0 解决办法

    int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        imageView.measure(w, h);
        int height =imageView.getMeasuredHeight();
        int width =imageView.getMeasuredWidth();
时间: 2024-08-29 19:21:28

setWidth()和setHeight()没反应的问题,onCreate()里面获取控件的高度是0的相关文章

Android下在onCreate中获取控件的宽度和高度(通过回调)

有时候需要在onCreate方法中知道某个View组件的宽度和高度等信息, 而直接调用View组件的getWidth().getHeight().getMeasuredWidth().getMeasuredHeight().getTop().getLeft()等方法是无法获取到真实值的,只会得到0. 这是因为View组件布局要在onResume回调后完成. 下面提供实现方法: 第一种: onGlobalLayout回调会在布局完成时自动调用 img1.getViewTreeObserver().

Android在onCreate中获取控件的宽高

在某些需求下,我们需要在onCreate的时候就获取到控件的宽高,但是如果直接用view.getWidth()或view.getHeight()会得到0.这是因为在onCreate执行的时候,控件还没有被绘制出来. 利用下面的方法可以获得控件的宽高: ViewTreeObserver vto = zoomImageView.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(

Android在OnCreate中获取控件的宽度和高度

原文链接:http://www.cnblogs.com/wt616/archive/2012/05/11/2496180.html 在Android中,有时需要对控件进行测量,得到的控件宽度和高度可以用来做一些计算.在需要自适应屏幕的情况下,这种计算就显得特别重要.另一方便,由于需求的原因,希望一进入界面后,就能得到控件的宽度和高度. 可惜的是,根据我的验证,利用网上转载的那些方法在OnCreate函数中获取到的仍然是0(希望搞技术的能自己验证过再转载),例如Measure方法之后调用getMe

android安卓onCreate方法中获取控件宽度高度

ViewTreeObserver vto = imageView.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); int height=imageView.getHeight();

android 关于setWidth()和setHeight()没反应的问题

在android开发过程中,对于控件的高度,宽度,虽然在xml中用android:layout_height="match_parent"设置了 高度(match_parent和fill_parent是一样的,2.2版本后就用match_parent代替fill_parent了.)但有时, 程序需要,必须在代码里,动态设置控制的高度或宽度. 我想当然的用setHeight(100);设置了高度,以为这样就可以了,但偏偏没有生效,google了好久都没结果,急得要命.后来在一篇文章找到了

Android在onCreate()中获得控件尺寸

@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        final ImageView imageView = (ImageView) findViewById(R.id.imageview);              int w = View.Mea

Android在onCreate()方法中动态获取TextView控件的高度

正好朋友项目里遇到了给写了个小Demo: 这个监听器看名字也知道了.就是在绘画完毕之前调用的,在这里面能够获取到行数.当然也能够获取到宽高等信息 package com.example.textviewtest; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Vie

在onCreate()中获取某个View的宽高

我们都知道,在Activity的onCreate()方法中调用View的getWidth()和getHeight()方法时,返回的值都是0,因为Activity调用setContentView方法只是创建了这个View,调用了这个View构造方法而已,其中的onMeasure()方法还没被调用,所以不能知道它的宽高.但是,View有一个公开的方法post(),允许我们通过参数Runnable,在子线程中获取View的宽和高. public class MainActivity extends A

在Activity中onCreate方法里面获取空间宽度和高度的新姿势

以前获取一个View的宽度和高度,总是在Activity中的onCreate方法中获取不到,那么我们怎么在onCreate方法中获取到控件的宽度和高度呢? 方法:用View中的post方法- 代码如下: public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConten