我正在使用代码创建
图像通过将其与先前创建的tv_visitor视图(已添加到相对布局)的
视图作为参数传递,布局通过以下方式获取:
ImageView
:ImageView vlogo = new ImageView(v.getContext());
RelativeLayout.LayoutParams vlogoParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
vlogoParams.addRule(RelativeLayout.ALIGN_TOP,tv_visitor.getId());
vlogoParams.addRule(RelativeLayout.ALIGN_LEFT, tv_visitor.getId());
vlogoParams.addRule(RelativeLayout.ALIGN_BOTTOM, tv_visitor.getId());
vlogo.setLayoutParams(vlogoParams);
vlogo.setImageResource(R.drawable.star);
rl.addView(vlogo);
图像通过将其与先前创建的tv_visitor视图(已添加到相对布局)的
TOP
和BOTTOM
对齐来“缩放”。但我认为它还没有被布置好(?)。在此代码之前使用.requestLayout并没有改变任何东西。
通过这样做,我将ImageView的高度设置为tv_visitor视图的高度。这是可以的。然而,宽度似乎保持原始状态。
问题就在这里。宽度没有进行缩放。我采取的方法是使.setAdjustViewBounds(true);无效。那么我该怎么办呢?
根据评论的要求,我提供更多信息:<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/news_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000"
android:scrollbars="none">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragment_news2">
<View
android:id="@+id/anchor_n"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ffbb00"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
</ScrollView>
视图作为参数传递,布局通过以下方式获取:
RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.fragment_news2);
tv_visitor View
是什么,它的LayoutParams
是什么,你确定原始图像没有任何填充吗? - blizzardRelativeLayout
中的一个错误引起的,它查询其子项的默认测量值(仅受其自身大小约束限制),然后强制进行自己的修改,而不查询给定子项约束的首选尺寸。作为解决方法,您可以将其包装在另一个容器中,这应该至少允许正确的对齐(尽管容器的宽度仍然相同)。硬编码父级的高度也可以解决此问题。稍后我会添加答案,以便更深入地探索。 - corsair992