1

我创建了课程customTextView extent TextView 并设置textAlignment为中心。在onMeasure我改变了textview宽度。

在那之后,textview align change to left and I used this.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); but it didn't change to center

4

2 回答 2

0

在您的课程中添加以下代码。

Runnable runnable = new Runnable() {
            public void run() {
              view.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
            }
   };

Handler handler = new Handler();
handler.postDelayed(runnable,1000);
于 2016-08-01T14:01:44.880 回答
0

像这样?

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        ImageView imageView = (ImageView)((Activity) context).findViewById(R.id.list_r);
        int imageViewHeight = imageView.getMeasuredWidth();

        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

        int final_width = parentWidth - 2*imageViewHeight;
        this.setMeasuredDimension(final_width, parentHeight);

        Runnable runnable = new Runnable() {
            public void run() {
                view.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
            }
        };

        Handler handler = new Handler();
        handler.postDelayed(runnable,1000);
    }
于 2016-08-01T14:12:18.897 回答