【发布时间】:2016-01-24 07:53:20
【问题描述】:
注意:我已经检查了this question,但没有任何答案有帮助。我重申这个问题是为了说明我的具体问题和需求。
我在我的 Android 应用中创建了一个自定义视图,用于显示即将发生的事件的数据(标题、位置、价格、描述等)。除了这个数据,这个视图上还显示了图标和封面照片(图标显示在视图左上角的ImageView中,并且封面修改为128的alpha,然后在视图的内容后面显示。这是我目前的布局:
<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(),设置图标图像和各种TextViews。此外,它将eventview_cover 的源设置为BitmapDrawable(以修改原始照片的alpha)。反过来,这会扩展视图的高度(我想避免)。视图的高度只能由图标的TextViews 和ImageView 的高度控制。这是EventView的代码:
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(),然后设置drawable,并使用bkg.getLayoutParams().height = height设置封面ImageView的高度,但是对getHeight()的调用返回0,因为视图在技术上不可见此时,封面照片是不可见的。视图应如下所示,其中背景图像被裁剪并居中:
tl;博士/摘要:
如何防止 ImageView 调整其父级的大小?
【问题讨论】:
标签: android android-layout resize imageview