Java 如何测量宽度和高度,然后在添加视图之前更改参数?
我想告诉我的头衔尊重他,他旁边有个约会,所以我想把他的宽度改成背景的宽度,减去日期。但有时getMeasuredWidth返回错误的值,或者仅返回0。我该怎么做呢?每个项目都会调用此方法Java 如何测量宽度和高度,然后在添加视图之前更改参数?,java,android,android-linearlayout,Java,Android,Android Linearlayout,我想告诉我的头衔尊重他,他旁边有个约会,所以我想把他的宽度改成背景的宽度,减去日期。但有时getMeasuredWidth返回错误的值,或者仅返回0。我该怎么做呢?每个项目都会调用此方法 private void populateNewsItems(int pos, List<NewsItem> mFeedItems) { NewsItem newsItem = mFeedItems.get(pos); View newsContainer = getActivity
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);
}
private void populateNewsItems(int pos,List mfeedeitems){
NewsItem NewsItem=mFeedItems.get(pos);
查看新闻容器=getActivity().GetLayoutFlater().inflate(R.layout.list\u item\u news,null);
TextView后台=(TextView)newsContainer.findViewById(R.id.background);
TextView title=(TextView)newsContainer.findViewById(R.id.title);
TextView colorBlock=(TextView)newsContainer.findViewById(R.id.colorBlock);
TextView日期=(TextView)newsContainer.findViewById(R.id.date);
TextView description=(TextView)newsContainer.findViewById(R.id.description);
title.setText(newsItem.getTitle());
date.setText(newItem.getDate());
description.setText(newsItem.getDescription());
标题.measure(View.MeasureSpec.unspected,View.MeasureSpec.unspected);
日期度量(View.MeasureSpec.unspected,View.MeasureSpec.unspected);
背景.measure(View.MeasureSpec.unspected,View.MeasureSpec.unspected);
RelativeLayout.LayoutParams paramsTitle=(RelativeLayout.LayoutParams)title.getLayoutParams();
paramsTitle.width=background.getMeasuredWidth()-date.getMeasuredWidth();
标题.setLayoutParams(Paramstite);
Log.e(“Log”,String.format(“背景宽度:%d;日期宽度:%d”,背景.getMeasuredWidth(),日期.getMeasuredWidth()));
linearLayout.addView(新闻容器);
}
它读取的日期宽度绝对正确。但这可能是因为在文本视图被填充之前是一样的。有一个比手动计算宽度简单得多的解决方案。在包含
R.id.title
和R.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中进行,但如果布局是真正动态的,则需要使用一个命令等待视图首次绘制完成,然后调整其大小。一些代码:
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();
}
});
尽管你的回答很有帮助,但它只解决了我一半的问题,因为我后来不得不以某种方式获得高度。但我可能也会实现这一点。非常感谢。如果我动态填充它,它会工作吗?另外,使用这个是否有效?因为一次大概会调用20次。我稍后会尝试,谢谢。通过XML静态布局比这种方法更有效。它确实会导致布局至少部分重复。但是如果你真的需要动态布局,这可能是一个必须付出的代价。再看看你的代码,我看不出任何明显的原因,你不应该仅仅通过一个加权的水平线性布局来包装标题和日期。因为我后来不得不根据标题文本视图的高度调整另一个视图的大小。我可以用另一种方法来解决这个问题,但这可能是解决我的另一个问题的最简单的方法。@TemporaryName您可以使用Scott的解决方案,并在获得高度后删除
GlobalLayoutListener
。