我需要实现自己的属性,如in com.android.R.attr
在官方文档中找不到任何内容,因此我需要有关如何定义这些attrs以及如何在我的代码中使用它们的信息.
我想为我的应用程序提供2个可选主题.为了做到这一点,我定义了一些属性,如下所示:
<attr format="color" name="item_background" />
Run Code Online (Sandbox Code Playgroud)
然后,我创建了两个主题,如下所示:
<style name="ThemeA">
<item name="item_background">#123456</item>
</style>
<style name="ThemeB">
<item name="item_background">#ABCDEF</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这种方法效果很好,允许我轻松地创建和修改几个主题.问题是它似乎只能在Views中使用,而不能在Drawables中使用.
例如,引用布局内View的值可以:
<TextView android:background="?item_background" />
Run Code Online (Sandbox Code Playgroud)
但是在Drawable中做同样的事情并不是:
<shape android:shape="rectangle">
<solid android:color="?item_background" />
</shape>
Run Code Online (Sandbox Code Playgroud)
运行应用程序时出现此错误:
java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
Run Code Online (Sandbox Code Playgroud)
如果不是?item_background我使用硬编码颜色,它可以工作,但这不允许我使用我的主题.我也试过?attr:item_background,但同样的事情发生了.
我怎么能这样做?为什么它在Views中有效但在Drawables中无效?我在文档中的任何地方都找不到这个限制......
在给我的布局充气时,我得到了这个例外:
E AndroidRuntime: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class <unknown>
E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
E AndroidRuntime: at com.myapp.view.MyRecyclerAdapter.onCreateViewHolder(MyRecyclerAdapter:80)
E AndroidRuntime: at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5288)
E AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4551)
E AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
E AndroidRuntime: at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
Run Code Online (Sandbox Code Playgroud)
日志中没有"由...引起",但我添加了代码来捕获异常并调用,getCause()直到它返回null,这是事件序列:
android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class <unknown>
android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
Error: Binary XML file line #11: …Run Code Online (Sandbox Code Playgroud) 我找不到任何关于以下差异的信息:
android:textColor="?attr/colorPrimary"
Run Code Online (Sandbox Code Playgroud)
对比
android:textColor="?colorPrimary"
Run Code Online (Sandbox Code Playgroud)
我读过“?attr”表示当前主题中指定的属性值,但没有“attr”它会给出相同的结果(=我的主题中定义的颜色)。它的行为与其他属性相似吗?
例如:
是否android:background="?attr/selectableItemBackground"
等于android:background="?selectableItemBackground"?
非常感谢。
我有一个A具有TextView 的自定义视图.我做了一个返回resourceIDTextView的方法.如果未定义文本,则默认情况下该方法将返回-1.我还有一个B从视图继承的自定义视图A.我的自定义视图的文本为"hello".当我调用方法来获取超类的属性时,我得到了-1.
在代码中还有一个例子,我可以如何检索值,但感觉有点hacky.
attrs.xml
<declare-styleable name="A">
<attr name="mainText" format="reference" />
</declare-styleable>
<declare-styleable name="B" parent="A">
<attr name="subText" format="reference" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
A级
protected static final int UNDEFINED = -1;
protected void init(Context context, AttributeSet attrs, int defStyle)
{
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.A, defStyle, 0);
int mainTextId = getMainTextId(a);
a.recycle();
if (mainTextId != UNDEFINED)
{
setMainText(mainTextId);
}
}
protected int getMainTextId(TypedArray a)
{
return a.getResourceId(R.styleable.A_mainText, UNDEFINED);
}
Run Code Online (Sandbox Code Playgroud)
B级
protected void init(Context context, AttributeSet attrs, …Run Code Online (Sandbox Code Playgroud) inheritance android android-custom-view declare-styleable android-attributes
我试图在我的视图中包含不同的布局,具体取决于父级Theme.
遵循这个想法:
attrs.xml
<attr name="themeLayout" format="reference" />
Run Code Online (Sandbox Code Playgroud)
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="themeLayout">@layout/layout_a</item>
</style>
<style name="AppThemeSecond" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="themeLayout">@layout/layout_b</item>
</style>
Run Code Online (Sandbox Code Playgroud)
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include layout="?attr/themeLayout" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我将得到以下异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.package/com.my.package.MainActivity}: android.view.InflateException: You must specifiy a valid layout reference. The layout ID ?attr/themeLayout is not valid.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: …Run Code Online (Sandbox Code Playgroud) 我注意到三星应用程序具有特定的形状; 例如,下面的图库,相机,手机,消息和互联网应用程序图标.
有没有办法只为三星设备指定图标?类似于新的Google Pixel如何android:roundIcon在清单中使用该属性?
我尝试在Samsung Developer文档中寻找一些东西,但似乎没有关于这个主题的任何内容.
这些设备确实有一种识别三星应用程序的方法,考虑到带有背景设置的图标 [ 设置 > 显示 > 图标背景 ]为所有其他应用程序启用类似形状的背景.例如,下面是Google Play商店图标.
但我不确定这是否是一个公开可用的选项.
如果三星采用Android Oreo的新自适应图标,这个问题将无关紧要; 但据我所知,他们还没有.
我有一个最小 sdk 16 到 23 的应用程序。我想尽可能多地使用材料设计。它也必须是全屏应用程序。包括 AppCompat 支持库。现在我有一些按钮的登录活动:
<Button
android:id="@+id/act_logon_btn_logon"
style="@style/ButtonDefault"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/act_logon_logon" />
Run Code Online (Sandbox Code Playgroud)
样式如下(values/styles.xml):
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar.FullScreen"></style>
<style name="AppTheme" parent="AppThemeBase"></style>
<style name="ButtonDefault" parent="Widget.AppCompat.Button">
<item name="android:textSize">?attr/font_medium</item>
</style>
<style name="FontStyle"></style>
<style name="FontStyle.Small">
<item name="font_small">12sp</item>
<item name="font_medium">14sp</item>
<item name="font_large">16sp</item>
</style>
<style name="FontStyle.Medium">
<item name="font_small">16sp</item>
<item name="font_medium">18sp</item>
<item name="font_large">20sp</item>
</style>
<style name="FontStyle.Large">
<item name="font_small">20sp</item>
<item name="font_medium">22sp</item>
<item name="font_large">24sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在值/属性中:
<declare-styleable name="FontStyle">
<attr name="font_small" format="dimension" />
<attr name="font_medium" format="dimension" /> …Run Code Online (Sandbox Code Playgroud) android android-inflate inflate-exception android-styles android-attributes
我想应用一些风格DatePicker.在平台中,attrs.xml我们可以看到以下属性DatePicker:
<declare-styleable name="DatePicker">
...
<!-- The text color for the selected date header text, ex. "2014" or
"Tue, Mar 18". This should be a color state list where the
activated state will be used when the year picker or day picker is
active.-->
<attr name="headerTextColor" format="color" />
<!-- The background for the selected date header. -->
<attr name="headerBackground" />
...
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
虽然我可以参考android:headerBackground,但出乎意料的是我无法为android:headerTextColor属性做到这一点.所以遵循以下代码styles.xml:
<style name="MyDatePickerStyle" parent="@android:style/Widget.Material.DatePicker">
<item name="android:headerBackground">@color/red</item> …Run Code Online (Sandbox Code Playgroud) android datepicker android-resources android-styles android-attributes
我有一个包含多个皮肤的应用程序。在我定义的一个皮肤中android:colorBackground,然后我想在 XML 布局中将此颜色设置为 ViewGroup 背景颜色。我该怎么做?是否可以?或者它只是系统使用的某些属性?
<style name="SkinDefault" parent="@style/_SkinXYZ">
<item name="android:colorBackground">@color/skin_default_color_background</item>
<item name="colorOnBackground">@color/skin_default_color_on_background</item>
</style>
Run Code Online (Sandbox Code Playgroud) android android-theme android-attributes material-components-android