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

时间:2015-04-08 02:09:00

标签: java android android-linearlayout

我试图告诉我的头衔尊重他旁边的日期,所以我想将他的宽度改为背景,减去日期。但有时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);
}

它读取日期宽度绝对正确。但这可能是因为在文本视图填满之前它是一样的。

2 个答案:

答案 0 :(得分:1)

比尝试手动计算宽度有一个更简单的解决方案。在包含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.

这个顺序实际上很重要。您希望在日期之后列出标题,以便在标题视图中引用日期视图。如果您有任何疑问和好运,请告诉我们!

答案 1 :(得分:1)

当您要求维度时,您的视图尚未实际绘制。如果您的布局是静态的,最好在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();
    }
});