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

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

我想告诉我的标题尊重他旁边有一个日期,所以我想将他的宽度更改为背景的宽度,减去日期。但有时 getMeasuredWidth returns 的值是错误的,或者只是 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();
    }
});