我有一个ImageView,它已初始化并以编程方式添加到从LinearLayout派生的自定义视图中。自定义视图在XML布局中引用。
图像资源是使用SetImageResource设置的,其容器视图状态最初是不可见的(我也尝试过使用消失的视图状态)。
事件发生后,容器的可见性在主UI线程上设置为可见视图状态。
期望
将容器的可见性设置为“可见”后,我希望能正确显示图像,但是我看不到图像。
实际
如果将容器的初始视图状态设置为不可见,那么当可见性切换为可见时,我会看到ImageView占据了预期的宽度和高度(使用布局边界),但是没有可见的图像。
如果我将容器的初始视图状态设置为走了,那么当可见性切换为可见时,我看不到ImageView占用了预期的宽度和高度(使用布局边界)并且没有可见的图像。
将容器LinearLayout的可见性设置为可见时,为什么没有出现ImageView图像?
下面是我的自定义类(图像下方显示了两个标题):
using System;
using System.Linq.Expressions;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Support.V4.Content;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Views;
using Android.Widget;
using Orientation = Android.Widget.Orientation;
namespace ShrimpleSearch.Components
{
public class CaptionImageView : LinearLayout
{
private static TypedArray ReadStyleAttributes(
Context context
, IAttributeSet attributeSet
) => context.ObtainStyledAttributes(
attributeSet
, Resource.Styleable.CaptionImageView
, 0
, 0
);
private static int GetImageResource(
TypedArray styleAttributes
) => styleAttributes.GetResourceId(
Resource.Styleable.CaptionImageView_drawableSrc
, 0
);
private static float GetDrawableMarginBottom(
TypedArray styleAttributes
) => styleAttributes.GetDimensionPixelSize(
Resource.Styleable.CaptionImageView_drawableMarginBottom
, 0
);
private static Color GetPrimaryTextColor(
TypedArray styleAttributes
) => styleAttributes.GetColor(
Resource.Styleable.CaptionImageView_primaryTextColor
, Color.Black
);
private static string GetPrimaryText(
TypedArray styleAttributes
) => styleAttributes.GetString(Resource.Styleable.CaptionImageView_primaryText);
private static Typeface GetPrimaryFontFamily(
TypedArray styleAttributes
) => styleAttributes.GetFont(Resource.Styleable.CaptionImageView_primaryTextFontFamily);
private static float GetPrimaryLetterSpacing(
TypedArray styleAttributes
) => styleAttributes.GetFloat(
Resource.Styleable.CaptionImageView_primaryLetterSpacing
, 0f
);
private static float GetPrimaryTextSize(
TypedArray styleAttributes
) => styleAttributes.GetDimensionPixelSize(
Resource.Styleable.CaptionImageView_primaryTextSize
, 0
);
private static float GetPrimaryTextBottomMargin(
TypedArray styleAttributes
) => styleAttributes.GetDimensionPixelSize(
Resource.Styleable.CaptionImageView_primaryTextBottomMargin
, 0
);
private static Color GetSecondaryTextColor(
TypedArray styleAttributes
) => styleAttributes.GetColor(
Resource.Styleable.CaptionImageView_secondaryTextColor
, Color.Black
);
private static float GetSecondaryLetterSpacing(
TypedArray styleAttributes
) => styleAttributes.GetFloat(
Resource.Styleable.CaptionImageView_secondaryLetterSpacing
, 0f
);
private static float GetSecondaryTextSize(
TypedArray styleAttributes
) => styleAttributes.GetDimensionPixelSize(
Resource.Styleable.CaptionImageView_secondaryTextSize
, 0
);
private static float GetSecondaryTextHorizontalPadding(
TypedArray styleAttributes
) => styleAttributes.GetDimensionPixelSize(
Resource.Styleable.CaptionImageView_secondaryTextHorizontalPadding
, 0
);
private static Typeface GetSecondaryFontFamily(
TypedArray styleAttributes
) => styleAttributes.GetFont(Resource.Styleable.CaptionImageView_secondaryTextFontFamily);
private static string GetSecondaryText(
TypedArray styleAttributes
) => styleAttributes.GetString(Resource.Styleable.CaptionImageView_secondaryText);
private static float GetPrimaryTextLineSpacing(
TypedArray styleAttributes
) => styleAttributes.GetFloat(Resource.Styleable.CaptionImageView_primaryTextLineSpacing, 1f);
private static float GetSecondaryTextLineSpacing(
TypedArray styleAttributes
) => styleAttributes.GetFloat(Resource.Styleable.CaptionImageView_secondaryTextLineSpacing, 1f);
private static AppCompatImageView CreateImageView(
Context context
, TypedArray styleAttributes
, IAttributeSet attributeSet
)
{
var layoutParameters = new LayoutParams(
ViewGroup.LayoutParams.MatchParent
, ViewGroup.LayoutParams.WrapContent
) {Weight = 1};
layoutParameters.SetMargins(
0
, 0
, 0
, (int) GetDrawableMarginBottom(styleAttributes)
);
var imageView = new AppCompatImageView(context, attributeSet)
{
Id = GenerateViewId(),
LayoutParameters = layoutParameters
};
imageView.SetImageResource(GetImageResource(styleAttributes));
return imageView;
}
private static AppCompatTextView CreatePrimaryCaptionTextView(Context context, TypedArray styleAttributes)
{
var layoutParams = new LayoutParams(
ViewGroup.LayoutParams.MatchParent
, ViewGroup.LayoutParams.WrapContent
) {Weight = 0};
layoutParams.SetMargins(
0
, 0
, 0
, (int) GetPrimaryTextBottomMargin(styleAttributes)
);
var textView = new LetterSpacingTextView(context)
{
Id = GenerateViewId(),
Text = GetPrimaryText(styleAttributes),
LetterSpacing = GetPrimaryLetterSpacing(styleAttributes),
TextAlignment = TextAlignment.Center,
Typeface = GetPrimaryFontFamily(styleAttributes),
LayoutParameters = layoutParams
};
textView.SetLineSpacing(0, GetPrimaryTextLineSpacing(styleAttributes));
textView.SetTextSize(ComplexUnitType.Px, GetPrimaryTextSize(styleAttributes));
textView.SetTextColor(GetPrimaryTextColor(styleAttributes));
return textView;
}
private static AppCompatTextView CreateSecondaryCaptionTextView(Context context, TypedArray styleAttributes)
{
var layoutParams = new LayoutParams(
ViewGroup.LayoutParams.MatchParent
, ViewGroup.LayoutParams.WrapContent
) {Weight = 0};
var horizontalPadding = (int) GetSecondaryTextHorizontalPadding(styleAttributes);
layoutParams.SetMargins(
horizontalPadding
, 0
, horizontalPadding
, 0
);
var textView = new LetterSpacingTextView(context)
{
Id = GenerateViewId(),
Text = GetSecondaryText(styleAttributes),
LetterSpacing = GetSecondaryLetterSpacing(styleAttributes),
TextAlignment = TextAlignment.Center,
Typeface = GetSecondaryFontFamily(styleAttributes),
LayoutParameters = layoutParams
};
textView.SetLineSpacing(0, GetSecondaryTextLineSpacing(styleAttributes));
textView.SetTextSize(ComplexUnitType.Px, GetSecondaryTextSize(styleAttributes));
textView.SetTextColor(GetSecondaryTextColor(styleAttributes));
return textView;
}
protected CaptionImageView(
IntPtr javaReference
, JniHandleOwnership transfer
) : base(javaReference, transfer) => Expression.Empty();
public CaptionImageView(Context context) : base(context) => Expression.Empty();
public CaptionImageView(
Context context
, IAttributeSet attrs
) : base(context, attrs) => Init(context, attrs);
public CaptionImageView(
Context context
, IAttributeSet attrs
, int defStyleAttr
) : base(context, attrs,
defStyleAttr) => Init(context, attrs);
public CaptionImageView(
Context context
, IAttributeSet attrs
, int defStyleAttr
, int defStyleRes
) : base(context, attrs, defStyleAttr, defStyleRes) => Init(context, attrs);
private void Init(Context context, IAttributeSet attrs)
{
var styleAttributes = ReadStyleAttributes(context, attrs);
var imageView = CreateImageView(context, styleAttributes, attrs);
var primaryCaptionTextView = CreatePrimaryCaptionTextView(context, styleAttributes);
var secondaryCaptionTextView = CreateSecondaryCaptionTextView(context, styleAttributes);
Orientation = Orientation.Vertical;
AddView(imageView);
AddView(primaryCaptionTextView);
AddView(secondaryCaptionTextView);
}
}
}