我正在尝试将配置(例如URL/etc)放入资源文件夹中以供实用程序类使用.但是,我不想从各地的活动中传递Context.我希望能够通过路径名访问资源(似乎资产/是为此用途设计的),而不使用上下文来访问资源.
在这种特殊情况下,我希望单例在实例化时在配置中使用某些东西.在实例化期间,除了那一次之外,它不需要任何资源.因此,每次调用getInstance()时都必须传入一个Context,这将是一个完全的浪费.
此外,这特定于App的配置,不应存储在共享系统文件或类似的任何内容中.
如何存储3.1416资源中的小数值?在XML中写什么以及如何在Java代码中检索它?
getFraction()各州的文件:
public float getFraction (int id, int base, int pbase)检索特定资源ID的小数单位.
参数
base此分数的基值.换句话说,标准分数乘以该值.
pbase此分数的父基值.换句话说,父分数(nn%p)乘以该值.返回
属性小数值乘以适当的基值
这个答案显示了一个简单的百分比示例,而没有详细说明参数的含义.
拥有此自定义视图MyView我定义了一些自定义属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="normalColor" format="color"/>
<attr name="backgroundBase" format="integer"/>
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
并在布局XML中将它们分配如下:
<com.example.test.MyView
android:id="@+id/view1"
android:text="@string/app_name"
. . .
app:backgroundBase="@drawable/logo1"
app:normalColor="@color/blue"/>
Run Code Online (Sandbox Code Playgroud)
起初我以为我可以backgroundBase使用以下方法检索自定义属性:
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, defStyle, 0);
int base = a.getInteger(R.styleable.MyView_backgroundBase, R.drawable.blank);
Run Code Online (Sandbox Code Playgroud)
仅在未分配属性且R.drawable.blank返回默认值时才有效.
当app:backgroundBase赋值时抛出异常"无法转换为整数类型= 0xn",因为即使自定义属性格式将其声明为整数,它实际上引用了a Drawable,应该按如下方式检索:
Drawable base = a.getDrawable(R.styleable.MyView_backgroundBase);
if( base == null ) base = BitMapFactory.decodeResource(getResources(), R.drawable.blank);
Run Code Online (Sandbox Code Playgroud)
这很有效.
现在我的问题:
我真的不想Drawable从TypedArray中获取,我想要对应的整数id app:backgroundBase(在上面的示例中它将是 R.drawable.logo1).我怎么才能得到它?
我对这两个API有点困惑.
ResourcesCompat.getDrawable(Resources res,int id,Resources.Theme theme)
返回与特定资源ID关联的可绘制对象,并为指定的主题设置样式.将根据底层资源返回各种类型的对象 - 例如,纯色,PNG图像,可缩放图像等.
在API级别21之前,不会应用主题,此方法只需调用getDrawable(int).
AppCompatResources.getDrawable(Context context,int resId)
返回与特定资源ID关联的可绘制对象.
此方法支持在没有平台支持的设备上对矢量和动画矢量资源进行充气.
我想重写从Android的命名空间,例如布局文件R.layout.popup_menu_item_layout(这是从代码中引用的com.android.internal.R.layout.popup_menu_item_layout).通过说覆盖,我假设xml在项目中声明一个文件,该文件将优先于框架拥有的布局.
请注意,这只是一个示例布局,因此问题涉及sdk/platforms/android-XX/data/res/layout目录中存在的每个布局.
工具:覆盖
有一个未记录的 tools:override标签可用,它会覆盖特定的资源.请参阅此答案以获取示例,该示例将覆盖设计支持库中的值,而不是来自Android框架的值.
应用于tools:override="true"布局的根标签不会生效.
XML布局参考 - refs.xml
正如在描述这个帖子,声明refs.xml文件/values/与以下内容目录:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="layout" name="activity_main">@layout/activity_second</item>
</resources>
Run Code Online (Sandbox Code Playgroud)
将参考activity_second.xml一次activity_main.xml使用.有一个答案建议使用这种技术来替代Snackbar布局.
这也不会生效.
有没有合法的方法来覆盖/替换android包中的布局文件?
我刚刚开始学习和使用“Android Studio”软件并编辑项目。我们花了很长时间才弄清楚如何使用它。我刚刚修复了许多错误,现在我知道了很多关于错误的事情,但是一件事发生在我身上,这让我发疯了!我无法修复这个错误,也不知道出了什么问题。这是错误:
> Task :app:processReleaseResources FAILED
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml"}],"original":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml: AAPT: error: style attribute 'android:attr/contextPopupMenuStyle' not found.\n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml"}],"original":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml: AAPT: error: style attribute 'android:attr/contextPopupMenuStyle' not found.\n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml"}],"original":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml: AAPT: error: style attribute 'android:attr/contextPopupMenuStyle' not found.\n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml"}],"original":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml: AAPT: error: style attribute 'android:attr/contextPopupMenuStyle' not found.\n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml"}],"original":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml: AAPT: error: style attribute 'android:attr/dialogCornerRadius' not found.\n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml"}],"original":"C:\\Users\\****\\.gradle\\caches\\transforms-2\\files-2.1\\b635fd2d4b36b21da676271add72c19b\\material-1.4.0-alpha01\\res\\values\\values.xml: AAPT: error: style attribute …Run Code Online (Sandbox Code Playgroud) 我在res/color/redeemlist_item_color.xml下的XML文件中定义了以下选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#FFFFFF" /> <!-- pressed -->
<item android:state_selected="true"
android:color="#FFFFFF" /> <!-- focused -->
<item android:color="#000000" /> <!-- default -->
</selector>
Run Code Online (Sandbox Code Playgroud)
我也有TextView一个ListView项目布局.当我设置android:textColor在此TextView以XML上述选择器,然后正确的颜色变化时,选择的项目.但是,我试图以下列方式以编程方式设置此资源:
holder.label.setTextColor(R.color.redeemlist_item_color);
Run Code Online (Sandbox Code Playgroud)
以这种方式设置时,颜色不再变化.可以通过TextView这种方式将选择器分配给a 吗?
有没有办法在XML中定义一个元素是资源引用的数组?例如(它不起作用,但它解释了我想要的):
<integer-array name="actions_images">
<item>@drawable/pencil</item>
<item>@drawable/pencil</item>
<item>@drawable/pencil</item>
<item>@drawable/pencil</item>
<item>@drawable/pencil</item>
<item>@drawable/pencil</item>
</integer-array>
Run Code Online (Sandbox Code Playgroud) 如何从此资源中获取字符串值"cancel"int:android.R.string.cancel?
谢谢
如果有一些方法可以colors.xml通过引用已经定义的另一种颜色来定义颜色,我一次又一次地想知道.好吧,我试了一下,确实,看起来你可以使用@ color/XXX引用其他颜色.
例如,一个colors.xml可能如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
…
<color name="primary_blue">#205081</color>
<color name="action_bar_text">@color/primary_blue</color>
…
</resources>
Run Code Online (Sandbox Code Playgroud)
但我不希望其他人primary_blue直接使用我的定义 - 没有上下文制作蓝色的东西是没有意义的.我提出的最好的解决方案是将"直接"颜色加上前缀primary_blue,然后注释它们不应该直接使用.
有没有办法阻止使用这些直接颜色,同时仍然允许我的colors.xml文件引用它们?我想也许样式或apklib可以帮助我.
android ×10
java ×2
xml ×2
aapt ×1
android-xml ×1
build.gradle ×1
colors ×1
gradle ×1
string ×1