注意:我已检查了this question,但没有一个答案有帮助。我正在重述这个问题以说明我的具体问题和需求。
我在我的Android应用中制作了一个自定义视图,显示即将发生的事件(标题,位置,价格,描述等)的数据。除此数据外,此视图还显示图标和封面照片(图标显示在视图左上角的ImageView
中,并且封面被修改为具有{{alpha} 1}},然后在后面显示视图的内容。到目前为止,这是我的布局:
128
当<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/eventview_main" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:background="@color/color_black" android:elevation="16dp">
<!-- OBJECT IN QUESTION -->
<ImageView
android:id="@+id/eventview_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="centerCrop" />
<ImageView android:id="@+id/eventview_icon"
android:layout_width="100dp" android:layout_height="100dp"
android:layout_marginLeft="@dimen/half_margin"
android:layout_marginRight="@dimen/half_margin"
android:layout_marginTop="@dimen/half_margin" android:elevation="8dp" />
<TextView android:id="@+id/eventview_title"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignLeft="@+id/eventview_price"
android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:layout_marginTop="@dimen/half_margin"
android:layout_toEndOf="@+id/eventview_icon" android:layout_toRightOf="@+id/eventview_icon"
android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/color_white" />
<TextView android:id="@+id/eventview_location"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
android:layout_below="@+id/eventview_price" android:layout_toEndOf="@+id/eventview_icon"
android:layout_toRightOf="@+id/eventview_icon" android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/color_white" />
<TextView android:id="@+id/eventview_price"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
android:layout_below="@+id/eventview_title" android:layout_toEndOf="@+id/eventview_icon"
android:layout_toRightOf="@+id/eventview_icon" android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/color_white" />
<TextView android:id="@+id/eventview_shortdesc"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignLeft="@+id/eventview_title"
android:layout_alignParentEnd="true" android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true" android:layout_alignStart="@+id/eventview_title"
android:layout_below="@+id/eventview_icon"
android:layout_marginBottom="@dimen/half_margin"
android:layout_marginLeft="@dimen/half_margin" android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/color_white" android:width="0dip" />
</RelativeLayout>
实例化我的视图时,它必须提供数据源(自定义类)。然后(在Activity
下)调用方法setSource()
,该方法设置图标图像和各种updateLayout()
。此外,它会将TextView
的来源设置为eventview_cover
(以修改原始照片的字母)。反过来,这会扩展视图的高度(我想避免)。视图的高度应仅由BitmapDrawable
的高度和图标的TextView
决定。这是ImageView
的代码:
EventView
这一切都有效,但它会调整视图大小以便为封面照片腾出空间,这也是我想要避免的。我在设置drawable之前尝试使用public class EventView extends RelativeLayout {
private MergeEvent src = null;
public MergeEvent getSource() {
return src;
}
protected void setSource(MergeEvent e) {
src = e;
//The data was updated, so we need to update the view!
updateLayout();
}
public EventView(MergeEvent src, Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setSource(src);
}
public EventView(MergeEvent src, Context context, AttributeSet attrs) {
super(context, attrs);
setSource(src);
}
public EventView(MergeEvent src, Context context) {
super(context);
setSource(src);
}
protected void updateLayout() {
if (getSource() != null) {
//Get source data.
MergeEvent src = getSource();
//Set the layout.
View.inflate(getContext(), R.layout.eventview, this);
//Get references to the TextViews and ImageViews
TextView title = (TextView)findViewById(R.id.eventview_title),
price = (TextView)findViewById(R.id.eventview_price),
location = (TextView)findViewById(R.id.eventview_location),
shortdesc = (TextView)findViewById(R.id.eventview_shortdesc);
ImageView icon = (ImageView)findViewById(R.id.eventview_icon),
cover = (ImageView)findViewById(R.id.eventview_cover);
icon.setImageBitmap(src.getIcon());
title.setText(src.getTitle());
price.setText("$" + src.getPrice());
location.setText(src.getLocation());
shortdesc.setText(src.getShortDescription());
//Create a new BitmapDrawable from the source's cover (a Bitmap)
BitmapDrawable bd = new BitmapDrawable(getResources(), src.getCover());
bd.setAlpha(128);
bkg.setImageDrawable(bd);
requestLayout();
}
}
}
,然后设置drawable,并使用getHeight()
设置封面ImageView
的高度,但调用{ {1}}已返回bkg.getLayoutParams().height = height
,因为此时视图在技术上不可见,因此封面照片不可见。视图应该如下所示,背景图像被裁剪并居中:
如何阻止getHeight()
调整其父级的大小?
答案 0 :(得分:0)
我在另一个与我非常相似的问题上找到了this answer:
这需要使用代码完成。您需要在屏幕渲染后几毫秒调用这些大小的API。因此,如果您在100毫秒后调用它,使用已呈现的任何视图的
postDelayed
方法,您将获得大小。
使用它,我将updateLayout()
方法修改为:
protected void updateLayout() {
if (getSource() != null) {
MergeEvent src = getSource();
View.inflate(getContext(), R.layout.eventview, this);
TextView title = (TextView)findViewById(R.id.eventview_title);
TextView price = (TextView)findViewById(R.id.eventview_price);
TextView location = (TextView)findViewById(R.id.eventview_location);
TextView shortdesc = (TextView)findViewById(R.id.eventview_shortdesc);
ImageView i = (ImageView)findViewById(R.id.eventview_icon);
i.setImageBitmap(src.getIcon());
title.setText(src.getTitle());
price.setText("$" + src.getPrice());
location.setText(src.getLocation());
shortdesc.setText(src.getShortDescription());
//use postDelay to set the ImageView's source and max size after 100 miliseconds (plenty of time for the UI to be drawn)
this.postDelayed(new Runnable() {
@Override
public void run() {
BitmapDrawable bd = new BitmapDrawable(getResources(), src.getCover());
bd.setAlpha(128);
ImageView bkg = (ImageView)findViewById(R.id.eventview_cover);
bkg.setMaxHeight(getHeight());
bkg.setImageDrawable(bd);
}
}, 100);
requestLayout();
}
}