简体   繁体   English

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

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

I'm trying to tell my title to respect that there's a date next to him, so I want to change his width to that of the background, minus the date. 我想告诉我的头衔,要尊重他旁边有个日期,因此我想将他的宽度改为背景的宽度,减去日期。 But sometimes getMeasuredWidth returns wrong values, or just 0. How do I go about doing this? 但是有时getMeasuredWidth返回错误的值,或者只是返回0。如何执行此操作? This method is called for every item. 每个项目都会调用此方法。

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);
}

It reads the date width absolutely correct. 它读取的日期宽度绝对正确。 But that's probably because it's the same before that textview gets filled. 但这可能是因为在填充textview之前是相同的。

There is a much simpler solution than trying to manually calculate the width. 比尝试手动计算宽度要简单得多的解决方案。 In your xml document containing R.id.title and R.id.background , try structuring the views a little something like this: 在包含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.

The ordering in this actually does matter. 排序实际上很重要。 You want to list the title after the date so you can reference the date view in the title view. 您希望在日期之后列出标题,以便可以在标题视图中引用日期视图。 Let me know if you have any more questions and good luck! 让我知道您是否还有其他疑问和好运!

At the point that you're asking for the dimensions, your view hasn't been actually drawn yet. 在您要求尺寸时,实际上尚未绘制视图。 If your layout is static, it's best to do it in the XML, but if it's truly dynamic, then you'll want to use an OnGlobalLayoutListener to wait until the view has been first drawn and then resize it. 如果您的布局是静态的,则最好在XML中完成,但如果它是真正的动态,那么您将需要使用OnGlobalLayoutListener来等待,直到首先绘制视图,然后再调整其大小。 Some code: 一些代码:

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