【问题标题】:android: setting a button programmatically using setX and setYandroid:使用 setX 和 setY 以编程方式设置按钮
【发布时间】:2013-11-28 22:23:05
【问题描述】:
public class Game_collecting_view extends View 
{
    Button image_boy;
    private static final int BOY_DIAMETER = 200; // initial spot size
    int boy_width =0;
    int boy_height =0;

   public void setGame_collecting(Game_collecting mGame_collecting)
   {
       this.mGame_collecting = mGame_collecting;
   }    

   // constructs a new View
   public Game_collecting_view(Context context, RelativeLayout parentLayout)
   {    
       super(context);
      resources = context.getResources(); // save Resources for loading external values
      layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      // get references to various GUI components
      relativeLayout = parentLayout;
      spotHandler = new Handler(); // used to add spots when game starts

   } 

   @Override
   protected void onSizeChanged(int width, int height, int oldw, int oldh)
   {
      viewWidth = width; // save the new width
      viewHeight = height; // save the new height
   } 

   public void set_boy()
   {
          final Button boy = (Button) layoutInflater.inflate(R.layout.untouched, null);
          boy.setX(viewWidth /2); 
          boy.setY(viewHeight - BOY_DIAMETER); 
          boy.setPadding(0,0,0,0);
          boy.setBackgroundResource(R.drawable.blue);         
          boy.setLayoutParams(new RelativeLayout.LayoutParams(BOY_DIAMETER, BOY_DIAMETER));
          relativeLayout.addView(boy); // add spot to the screen

          Toast.makeText(getContext(), "set_boy\nviewWidth=" +viewHeight +"\nviewHeight=" +viewHeight, Toast.LENGTH_SHORT).show();

          boy.setOnClickListener
          ( 
             new OnClickListener()
             {            
                public void onClick(View v)
                {
                   touchedSpot(boy); 
                } 
             }  
          ); 
   }

   public void resume(Context context)
   {
         resetGame();
   } 

   public void resetGame()
   {
      for (int i = 1; i <= INITIAL_SPOTS; ++i)
      {
          spotHandler.postDelayed(addSpotRunnable, i * SPOT_DELAY);
          generate_text();
      }          
      set_boy();
   } 

   private Runnable addSpotRunnable = new Runnable()
   {
      public void run()
      {
         addNewSpot(); // add a new spot to the game
      } 
   }; 

目标:

我想在屏幕底部中间设置男孩图标。 男孩图标就是这样设置的,以后的动态界面(向右滑动屏幕男孩图标会向右移动,反之亦然)

观察:

toast 报告 viewWidth 和 viewHeight =0,男孩图标出现在 0,0(左上角)。如果我设置 setY(viewHeight + BOY_DIAMETER),男孩图标将位于 (0, 200)。

问题:

我想问一下为什么viewWidth和viewHeight都报0。如何立即调用onSizeChanged使得男孩图标可以设置在屏幕的底部中心?

谢谢!!

【问题讨论】:

  • stackoverflow.com/questions/19932253/… 在这里查看我的答案
  • 您在元素布局在屏幕上之前太早询问尺寸。
  • @njzk2:你说得对,我现在使用getViewTreeObserver() 来推导ViewWidthViewHeight,然后再通过setXsetY。然而男孩图标仍然位于 (0,0)。我试过如果使用setX(720/2)setY(1134-200) 它可能位于所需的位置。这意味着在设置男孩图标之前尚未收到viewWidthviewHeight....那样我怎么能推迟定位男孩图标? using Handler 如何知道何时检索到维度?

标签: android view android-relativelayout coordinate


【解决方案1】:

在 Android 计算出它们的位置之前,该位置为空。您可以像这样检索高度和宽度:

image_boy.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            int height = image_boy.getHeight();
            int width = image_boy.getWidth(); 
            //these values won't be null
        }
    });

【讨论】:

  • 我已经使用这个getViewTreeObserver() 来获取相对布局的高度和宽度,现在它可以工作了=)
【解决方案2】:

设置视图位置的布局参数

【讨论】:

  • 设置布局参数? boy.setLayoutParams(new RelativeLayout.LayoutParams(BOY_DIAMETER, BOY_DIAMETER));
猜你喜欢
  • 2013-05-22
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
相关资源
最近更新 更多