1

我正在尝试View在其 parent 的中心(垂直)制作 a View,即 a RelativeLayout

我试过了View.setGravity(Gravity.VERTICAL);

但它并不完美,视图的位置略低于中心。我的代码一定有问题,但我似乎无法发现阻止视图位于中心的错误。

        ....

        initArrayViews[a] = new RelativeLayout(context);
        initArrayViews[a].setBackgroundResource(R.drawable.bg);
        initArrayViews[a].setLayoutParams(screensize);

        inInit.addView(initArrayViews[a]);

         RelativeLayout theone = new RelativeLayout(context);
         theone.setBackgroundResource(R.drawable.layer1);
         theone.setLayoutParams(screensize);
         initArrayViews[a].addView(theone);
         theone.setGravity(Gravity.CENTER_VERTICAL);
         params = new LinearLayout.LayoutParams(this.getWidth(), this.getHeight());

         LinearLayout X = new LinearLayout(context);
         X.setLayoutParams(params);
         X.setBackgroundResource(R.drawable.roundr);
         X.setOrientation(LinearLayout.VERTICAL);
         theone.addView(X);
4

0 回答 0