GVKun编程网logo

setWidth()按钮Android(android set)

15

对于想了解setWidth()按钮Android的读者,本文将提供新的信息,我们将详细介绍androidset,并且为您提供关于android@android:Theme.Dialog和@androi

对于想了解setWidth()按钮Android的读者,本文将提供新的信息,我们将详细介绍android set,并且为您提供关于android @android:Theme.Dialog 和 @android:Widget.Button找不到、Android getWidth () getHeight () 方法返回的值为 0、Android getWidth()在View的onDraw中返回0、android view getWidth 和 getHeight 的值为0的有价值信息。

本文目录一览:

setWidth()按钮Android(android set)

setWidth()按钮Android(android set)

我已经在RelativeLayout中创建了一个Button,我想设置它的大小.但我只能设置一个比原来更大的尺寸,例如我可以将其调整为200dp但不调整为20dp.

有解决方案吗?

这是我的代码

Button b = new Button(getApplicationContext());
myLayout.addView(b);
b.setWidth(200); // Work
b.setWidth(20); // Don't work

谢谢

解决方法:

这是因为默认情况下Button的最小宽度为64dip.在设置宽度之前将其设置为0.

b.setMinimumWidth(0);

android @android:Theme.Dialog 和 @android:Widget.Button找不到

android @android:Theme.Dialog 和 @android:Widget.Button找不到

android @android :Theme.Dialog 和 @android :Widget.Button找不到是什么回事?

Android getWidth () getHeight () 方法返回的值为 0

Android getWidth () getHeight () 方法返回的值为 0

使用一个 view 的 getWidth () getHeight () 方法来获取该 view 的宽和高,返回的值却为 0。如果这个 view 的长宽很确定不为 0 的话,那很可能是你过早的调用这些方法,也就是说在这个 view 被加入到 rootview 之前你就调用了这些方法,返回的值自然为 0.

解决该问题的方法有很多,主要就是延后调用这些方法。可以试着在 onWindowFocusChanged () 里面调用这些方法。

也可以使用

  Runnable runnable=new Runnable() {  
   @Override
   public void run() {
    initView();
   }
  };
  Handler handler=new Handler();
  handler.postDelayed(runnable, 10);

 

Android getWidth()在View的onDraw中返回0

Android getWidth()在View的onDraw中返回0

这是我的代码:

if (myView == null) {
        myView = new View(mContext) {
            @Override
            protected void onDraw(final Canvas canvas) {
                super.onDraw(canvas);
                final float width = (float)getWidth();
                ......
            }
        };

        addView(myView);
    }

    requestLayout();

虽然mContext的大小不为零,但我的代码中的宽度始终为0.任何人都可以建议我得到宽度为0的可能原因是什么?谢谢

解决方法

您必须在onSizeChanged处理程序中记录画布的尺寸,然后在onDraw处理程序中使用它们.

private int canvasWidth;
private int canvasHeight;

@Override
protected void onSizeChanged(int w,int h,int oldw,int oldh) {
    super.onSizeChanged(w,h,oldw,oldh);
    canvasWidth = w;
    canvasHeight = h;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    final float width = (float)canvasWidth;
    ...
}

android view getWidth 和 getHeight 的值为0

android view getWidth 和 getHeight 的值为0

The getWidth and getHeight methods will return 0 if the view has not yet been inflated. For example, if you are trying to access it in the onCreate of the Activity, you''ll get zero.

在UI 组件还未显示在界面之前调用getWidth和getHeight方法通常会得到0。所以不能在onCreate方法里获得组件的width和height.

可以通过以下两种方法获得:

float width=activity.getWindowManager().getDefaultDisplay().getWidth();
float height=activity.getWindowManager().getDefaultDisplay().getHeight();

或者重写onWindowFocusChanged,在这个方法里获取

public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		view.getHeight();
                view.getWidth();
	}


我们今天的关于setWidth()按钮Androidandroid set的分享已经告一段落,感谢您的关注,如果您想了解更多关于android @android:Theme.Dialog 和 @android:Widget.Button找不到、Android getWidth () getHeight () 方法返回的值为 0、Android getWidth()在View的onDraw中返回0、android view getWidth 和 getHeight 的值为0的相关信息,请在本站查询。

本文标签: