为什么android:foreground属性不起作用? [英] Why doesn't `android:foreground` attribute work?
问题描述
看看这个小型的Android应用程序:
Take a look at this small android app:
MainActivity.java:
MainActivity.java:
package io.github.gsaga.toucheventtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main:
activity_main:
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="@drawable/ic_launcher_background"
xmlns:android="http://schemas.android.com/apk/res/android" />
未显示android:foreground
指向的图像,但如果我将activity_main.xml
中的foreground
更改为src
或background
,则会显示该图像.此代码似乎遵循此处描述的说明:
The image pointed to by android:foreground
isn't displayed, but it appears if I change foreground
to src
or background
in activity_main.xml
. This code seems to follow the instructions described here:
https://developer.android.com/reference/android/view/View.html#attr_android:foreground
为什么android:foreground
标记在上面的代码中不起作用?
Why doesn't the android:foreground
tag work in above code?
注意:
minSdkVersion
是19
,我正在Android 5.1
(API level 22
)
推荐答案
简短答案
这是由于 bug 自API级别23开始存在于Android中.
Short answer
This is due to a bug which existing in Android since API level 23.
以下是与通过FrameLayout
引入的API级别的视图设置前景可绘制对象相关的所有XML属性和相应方法的列表.但是,这些后来又移至API级别23中的View
.
Here is the list of all the XML attributes and corresponding methods related to setting foreground drawables to views with the API level they are introduced through FrameLayout
. However, these are later moved into View
in API level 23.
╔════════════════════════════╦═════════════════════════════════════════════════╦═════════════╗
║ XML attribute ║ Method ║ Added in ║
║ ║ ║ (API level) ║
╠════════════════════════════╬═════════════════════════════════════════════════╬═════════════╣
║ android:foreground ║ setForeground(Drawable) ║ 1 ║
╠════════════════════════════╬═════════════════════════════════════════════════╬═════════════╣
║ android:foregroundGravity ║ setForegroundGravity(int gravity) ║ 1 ║
╠════════════════════════════╬═════════════════════════════════════════════════╬═════════════╣
║ android:foregroundTint ║ setForegroundTintMode(PorterDuff.Mode tintMode) ║ 21 ║
╠════════════════════════════╬═════════════════════════════════════════════════╬═════════════╣
║ android:foregroundTintMode ║ setForegroundTintMode(PorterDuff.Mode tintMode) ║ 21 ║
╚════════════════════════════╩═════════════════════════════════════════════════╩═════════════╝
-
Android文档说在API 1中添加了
setForeground(Drawable)
,在API级别21中将setForegroundTintList (ColorStateList tint)
和setForegroundTintMode (PorterDuff.Mode tintMode)
添加到了View
.实际上,它们一直存在于FrameLayout
中,直到它移入API 23.Android doc says
setForeground(Drawable)
is added in API 1 andsetForegroundTintList (ColorStateList tint)
andsetForegroundTintMode (PorterDuff.Mode tintMode)
are added in API level 21 toView
. Actually they were there inFrameLayout
until it moved in API 23.在API级别< 23,即使没有要求,您也会收到警告.您可以压制它.参见此.
In API level < 23, you will get a warning even though it is not required. You can just suppress it. See this.
现在看看这些属性如何在不同版本上工作.
Now take a look at how these properties work on different versions.
╔═══════════╦══════════════════╦══════════════════╗ ║ API level ║ By code ║ Using XML ║ ╠═══════════╬══════════════════╬══════════════════╣ ║ <23 ║ FrameLayout only ║ FrameLayout only ║ ╠═══════════╬══════════════════╬══════════════════╣ ║ >=23 ║ FrameLayout only ║ All views ║ ╚═══════════╩══════════════════╩══════════════════╝
当这些属性在API级别23中移到
View
时,它们对其进行了一些奇怪的修改,这可以称为错误.从XML加载属性时,它会检查View
是否为FrameLayout
,而该cc在我们可用于相同目的的方法中不存在.When these properties moved to
View
in API level 23, they did some strange modifications on it which can be called a bug. While loading properties from XML, it checks whether theView
is aFrameLayout
which is not present inside the methods we can use for the same purpose.View构造函数,API级别23:
View constructor, API level 23:
case R.styleable.View_foreground: if (targetSdkVersion >= Build.VERSION_CODES.M || this instanceof FrameLayout) { setForeground(a.getDrawable(attr)); } break;
这篇关于为什么android:foreground属性不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!