繁体   English   中英

如何测量宽度和高度,然后在添加视图之前更改参数?

[英]How to measure width and height, then change parameters before adding view?

我想告诉我的头衔,要尊重他旁边有个日期,因此我想将他的宽度改为背景的宽度,减去日期。 但是有时getMeasuredWidth返回错误的值,或者只是返回0。如何执行此操作? 每个项目都会调用此方法。

private void populateNewsItems(int pos, List<NewsItem> mFeedItems) {
    NewsItem newsItem = mFeedItems.get(pos);
    View newsContainer = getActivity().getLayoutInflater().inflate(R.layout.list_item_news, null);

    TextView background = (TextView) newsContainer.findViewById(R.id.background);
    TextView title = (TextView) newsContainer.findViewById(R.id.title);
    TextView colorBlock = (TextView) newsContainer.findViewById(R.id.colorBlock);
    TextView date = (TextView) newsContainer.findViewById(R.id.date);
    TextView description = (TextView) newsContainer.findViewById(R.id.description);

    title.setText(newsItem.getTitle());
    date.setText(newsItem.getDate());
    description.setText(newsItem.getDescription());

    title.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    date.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    background.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

    RelativeLayout.LayoutParams paramsTitle = (RelativeLayout.LayoutParams) title.getLayoutParams();
    paramsTitle.width = background.getMeasuredWidth() - date.getMeasuredWidth();
    title.setLayoutParams(paramsTitle);

    Log.e("LOG", String.format("background width:%d ; date width:%d", background.getMeasuredWidth(), date.getMeasuredWidth()));
    linearLayout.addView(newsContainer);
}

它读取的日期宽度绝对正确。 但这可能是因为在填充textview之前是相同的。

比尝试手动计算宽度要简单得多的解决方案。 在包含R.id.titleR.id.background xml文档中,尝试将视图结构如下:

<RelativeLayout // this is built to match the width of the background
 android:width="wrap_content"
 android:height="wrap_content"/>
     <TextView
      android:width="wrap_content"
      android:height="wrap_content"
      android:id="@+id/background"/>
     <RelativeLayout
      android:width="match_parent"
      android:height="wrap_content"/>
           <TextView
            android:width="wrap_content"
            android:height="wrap_content"
            android:id="@+id/date"
            android:layout_alignParentRight="true"/> // This aligns the date to the 
                                                     // right side of the view
           <TextView
            android:width="match_parent" // this fills the container which matches the width of background, currently
            android:height="wrap_content"
            android:id="@+id/title"
            android:layout_alignLeft="@id/date"/> // and this makes sure the view doesn't overlap the date.

排序实际上很重要。 您希望在日期之后列出标题,以便可以在标题视图中引用日期视图。 让我知道您是否还有其他疑问和好运!

在您要求尺寸时,实际上尚未绘制视图。 如果您的布局是静态的,则最好在XML中完成,但如果它是真正的动态,那么您将需要使用OnGlobalLayoutListener来等待,直到首先绘制视图,然后再调整其大小。 一些代码:

newsContainer.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        // title.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // date.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // background.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

        RelativeLayout.LayoutParams paramsTitle = (RelativeLayout.LayoutParams) title.getLayoutParams();
        // paramsTitle.width = background.getMeasuredWidth() - date.getMeasuredWidth();
        paramsTitle.width = background.getWidth() - date.getWidth();
        title.setLayoutParams(paramsTitle);
        title.requestLayout();
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM