我有以下ListView项:
目前一切都按预期工作,除了TextView(2)的宽度外,它应放在应有的位置。
由于我将RelativeLayout与一些wrap_contents一起使用,因此我使用onGlobalLayoutListener,因此我可以在View完成加载和渲染时访问MeasuredWidths和Heights。使用TextView2但是当我调试时,我得到了一些奇怪的结果。
第一次调用onGlobalLayout时,TextView2的measuredWidth应该是(375 px)。第二次,它是48 px(与高度相同),当我查看TextView2的字段时,它说:mMeasuredHeight: 375; mMeasuredWidth: 48
:S
我的布局位于这篇文章的底部。我的onGlobalLayoutListener是:
if(view.getViewTreeObserver().isAlive()){
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// This will be called once the layout is finished, prior to displaying it
// So we can change some widths and heights based on other View-Elements that are filled now
// (We couldn't do this in the XML itself since they weren't filled yet and we didn't knew the sizes yet.)
@Override
public void onGlobalLayout() {
if(holder != null){
...
// Change the height of the ProductName-TextView to match the Image and leave the width as is
int height = h.imageView.getMeasuredHeight();
int width = h.tvName.getMeasuredWidth();
* h.tvName.setLayoutParams(new RelativeLayout.LayoutParams(h.imageView.getMeasuredHeight(), h.tvName.getMeasuredWidth()));
...
}
// Since we don't want onGlobalLayout to continue forever, we remove the Listener here again.
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
*
是一个断点。第一次宽度为48,高度为375.第二次宽度为48,高度为48,如果我查看holder.textView2中的mMeasuredHeight和mMeasuredWidth字段,则h = 375,w = 48:S < / p>
这是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:id="@+id/left_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@layout/transparent_background"
android:contentDescription="@string/checkbox_content_description"
android:src="@drawable/checkbox_unchecked" />
<Space
android:id="@+id/filler_space_image"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="gone" />
</LinearLayout>
<TextView
android:id="@+id/tv_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/left_ll"
android:layout_toLeftOf="@+id/right_ll"
android:ellipsize="end"
android:singleLine="true"
android:gravity="center_vertical"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin" />
<EditText
android:id="@+id/et_result_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_product_name"
android:layout_toRightOf="@id/left_ll"
android:inputType="number"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin"
android:visibility="gone" />
<AutoCompleteTextView
android:id="@+id/actv_result_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/right_ll"
android:layout_toRightOf="@id/et_result_amount"
android:layout_below="@+id/tv_product_name"
android:ellipsize="end"
android:inputType="text"
android:singleLine="true"
android:visibility="gone" />
<TextView
android:id="@+id/tv_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_result_amount"
android:layout_toRightOf="@id/left_ll"
android:text="@string/tags"
android:gravity="center"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin"
android:visibility="gone" />
<Spinner
android:id="@+id/sp_tags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/actv_result_name"
android:layout_toRightOf="@id/tv_tags"
android:layout_toLeftOf="@id/right_ll"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin"
android:visibility="gone" />
<LinearLayout
android:id="@id/right_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_margin="@dimen/default_margin">
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<Space
android:id="@+id/filler_space_price"
android:layout_width="1dp"
android:layout_height="1dp"
android:visibility="gone" />
<ImageButton
android:id="@+id/btn_tags"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@android:drawable/ic_menu_manage"
android:contentDescription="@string/button_tags_content_description"
android:background="@layout/transparent_background"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
好吧,它(在我弄清楚之后)非常明显......而不是
h.tvName.setLayoutParams(new RelativeLayout.LayoutParams(
h.imageView.getMeasuredHeight(), h.tvName.getMeasuredWidth()));
我现在用
h.tvName.setLayoutParams(new RelativeLayout.LayoutParams(
h.tvName.getMeasuredWidth(), hh.imageView.getMeasuredHeight()));
你问的区别?我使用LayoutParams(height, width)
代替LayoutParams(width, height)
...