android 组件消失了,为什么Android没有明确声明它们时选择AppCompat组件?

简短回答“我应该从现在开始的活动扩展吗?”是否定的,您应该继续扩展AppCompatActivity,因为它向旧设备提供了向后兼容的功能。在AppCompatImageView的情况:它支持在旧版本的平台,包括兼容功能

一个ImageView的:

允许通过在ViewCompat背景色调方法及其背景的动态色彩。

允许使用backgroundTint和backgroundTintMode设置背景色调。

通过ImageViewCompat中的图像色调方法允许其图像的动态色调。

允许使用tint和tintMode设置图像色调。

此外,它还增加了与旧版Android版本的矢量绘制兼容性。

说明有关不一致

当您在布局中使用的ImageView由设置在顶层活动/对话框这将自动使用和程序兼容性。

所以,这并不意外。

它是如何工作

AppCompatActivity安装一个LayoutInflater.Factory2拦截的某些观点的通货膨胀。这个充气器的代码可以在AppCompatViewInflater.java中看到。

负责创建Views为AppCompatViewInflater#createView(View, String, Context, AttributeSet, boolean, boolean, boolean, boolean),正如你可以在这里看到它检查简单视图名(不带包前缀的话)的功能,并创建AppCompat*版本而不是:

public final View createView(View parent, final String name, @NonNull Context context,

@NonNull AttributeSet attrs, boolean inheritContext,

boolean readAndroidTheme, boolean readAppTheme, boolean wrapContext) {

final Context originalContext = context;

// ...

View view = null;

// We need to 'inject' our tint aware Views in place of the standard framework versions

switch (name) {

case "TextView":

view = new AppCompatTextView(context, attrs);

break;

case "ImageView":

view = new AppCompatImageView(context, attrs);

break;

case "Button":

view = new AppCompatButton(context, attrs);

break;

case "EditText":

view = new AppCompatEditText(context, attrs);

break;

case "Spinner":

view = new AppCompatSpinner(context, attrs);

break;

case "ImageButton":

view = new AppCompatImageButton(context, attrs);

break;

case "CheckBox":

view = new AppCompatCheckBox(context, attrs);

break;

case "RadioButton":

view = new AppCompatRadioButton(context, attrs);

break;

case "CheckedTextView":

view = new AppCompatCheckedTextView(context, attrs);

break;

case "AutoCompleteTextView":

view = new AppCompatAutoCompleteTextView(context, attrs);

break;

case "MultiAutoCompleteTextView":

view = new AppCompatMultiAutoCompleteTextView(context, attrs);

break;

case "RatingBar":

view = new AppCompatRatingBar(context, attrs);

break;

case "SeekBar":

view = new AppCompatSeekBar(context, attrs);

break;

}

if (view == null && originalContext != context) {

// If the original context does not equal our themed context, then we need to manually

// inflate it using the name so that android:theme takes effect.

view = createViewFromTag(context, name, attrs);

}

// ...

return view;

}

强制非程序兼容性的使用意见

因此,以迫使创建一个普通ImageView(无AppCompatImageView),而仍然使用AppCompatActivity你需要SPE cify完整的类名,例如:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@mipmap/test"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值