gpt4 book ai didi

android - RelativeLayout 在 onGlobalLayout 中更改高度并保留宽度以填充

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:23 27 4
gpt4 key购买 nike

我有以下 ListView 项目: enter image description here

目前,除了 TextView (2) 的宽度外,一切都按预期工作并放置在应有的位置。

因为我使用了带有一些 wrap_contents 的 RelativeLayout,所以我使用了 onGlobalLayoutListener,所以我可以在 View 完成加载和渲染时访问 MeasuredWidths 和 Heights。然而,使用 TextView2 时,我在调试时得到了一些奇怪的结果。

第一次调用 onGlobalLayout 时,TextView2 的 measuredWidth 是它应该的值(375 像素)。然而,第二次是 48 像素(与高度相同),当我查看 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

这是我的布局:

<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>

最佳答案

好吧,这(在我弄清楚之后)非常明显..而不是

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)...

关于android - RelativeLayout 在 onGlobalLayout 中更改高度并保留宽度以填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24930204/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com