【问题标题】:Getting end position of the textview and imageview with respect to top of the screen获取文本视图和图像视图相对于屏幕顶部的结束位置
【发布时间】:2013-09-02 17:52:06
【问题描述】:

我有一张位图,下面是时间线。

FIGURE 的右侧布局为例。

所有底部时间线(1、2、3...)与顶部的高度相同。

时间线是一个文本视图,它具有固定的布局高度和宽度,正如它在 xml 中定义的那样 就像时间线 1 被定义为:

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/HView"
android:layout_marginLeft="18dp"
android:layout_marginTop="345dp"
android:textSize="14sp"
android:text="1"
android:textColor="#000000" />

但是,位图的高度和宽度可能会因编程方式而有所不同。

因此,在某些情况下,位图高度会增加到足以与时间线重叠。换句话说, 位图的垂直位置相对于时间轴的垂直位置增加。

我想得到:

1)位图相对于屏幕顶部的结束垂直位置。

2)时间轴相对于屏幕顶部的结束垂直位置。

我尝试执行以下操作:

TextView bottomTimeLine = (TextView) view.findViewById(R.id.textView1);

bottomTimeLine.getHeight(); //returns 0.

bottomTimeLine.getBottom(); //returns 0.

ImageView img = new ImageView(getActivity());

img.setImageDrawable(getResources().getDrawable(R.drawable.disp_bg));

img.getHeight(); //returns 0.

img.getBottom(); //returns 0.

从代码中可以看出,getHeight()getBottom() 这两个方法都将高度返回为 0。

如何获取两者相对于单元格显示顶部的高度(查看结束位置)?

【问题讨论】:

    标签: android bitmap textview position


    【解决方案1】:

    希望对你有帮助

    @Override

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

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
    this.setMeasuredDimension(
            parentWidth / 2, parentHeight);
    

    }

    【讨论】:

      【解决方案2】:

      可以这样做:

      final TextView bottomTimeLine = (TextView) view.findViewById(R.id.textView1);
      
       final int[] timelineCoord = new int[2]; 
      
       final int[] imgCoord = new int[2]; 
      
      
      
      ViewTreeObserver vto = bottomTimeLine.getViewTreeObserver();
       vto.addOnGlobalLayoutListener((new OnGlobalLayoutListener() {
      
          @Override
          public void onGlobalLayout() {
      
      
              bottomTimeLine.getLocationOnScreen(timelineCoord);
      
          Log.d(" bottomTimeLine H ", ""+timelineCoord[1]);
      
          timelineHeight = timelineCoord[1];
          }
      }));
      
      
      ViewTreeObserver vt1 = img.getViewTreeObserver();
      vt1.addOnGlobalLayoutListener((new OnGlobalLayoutListener() {
      
          @Override
          public void onGlobalLayout() {
      
      
              img.getLocationOnScreen(imgCoord);
      
              imgHeight = imgCoord[1] + img.getHeight();
      
          Log.d("Img H ", ""+imgHeight);
      
      if(imgHeight < timelineHeight)
      {
      int heightDiff = imgHeight - timelineHeight ;
      
      heightDiff = heightDiff + 3;
      
      img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, heightDiff));
      }
          }
      }));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-10
        • 2013-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多