说我想R.drawable.*根据字符串的值动态加载图像文件
有没有办法做到这一点?好像我需要静态地引用任何内容R.
我正在为Android 平板电脑做一个Android应用程序,它具有完整的图像背景,但我不能使用9补丁,所以我需要背景尽可能接近窗口大小.
我已经使用了测试drawable-ldpi,drawable-mdpi等...文件夹,但我想要的图像,以适合窗口,这是行不通的那么好.
现在我正在尝试使用drawable-wxxxdp文件夹来获得大小接近实际窗口大小的图像.
所以我有这个文件夹结构(每个文件夹都有不同的图像,即:不同文件夹中相同大小的图像不同):
drawable-w800dp-mdpi // 800x600px image
drawable-w800dp-hdpi // 800x600px image
drawable-w1024dp-mdpi // 1024x600px image
drawable-w1024dp-hdpi // 1024x600px image
drawable-w1280dp-mdpi // 1280x800 image
drawable-w1280dp-hdpi // 1280x800 image
drawable-w1280dp-xhdpi // 1280x800 image
Run Code Online (Sandbox Code Playgroud)
该问题是,当我测试应用程序在不同的平板电脑并不总是与预期的文件夹取图像.
例如:在1280x800px和160 dpis平板电脑中,报告(使用DisplayMetrics)窗口大小为1280x752,它使用drawable-w1280dp-mdpi文件夹中的1280x800px图像并从顶部和底部剪切一些图像.但是在1280x800px和213dpis平板电脑
上报告的窗口大小为1280x736它会从drawable-w800dp-hdpi... 获取图像...我希望从其他文件夹中获取它,例如drawable-w1024dp文件夹之一...但不是从那个... .
另一方面,如果我尝试使用像1024x600px和160dpis这样的配置来报告窗口大小为1024x552的模拟器,它会从drawable-w1024dp-hdpi文件夹中获取图像,但它会将其缩小并将其居中放在屏幕上,因此我以非常大的边框 …
问题是,无论我在何处或如何调用此布局的组件,它们始终返回null.
setView(inflater.inflate(R.layout.search_layout, null))
Run Code Online (Sandbox Code Playgroud)
这很好用.它显示了里面的布局Dialog,但是,子节点总是返回为null findViewById(R.id.some_search_layout_children).
我试图清理我的项目多次,试图实现另一个类我Dialog,叫findViewById()我的主要成员Activity,内部initSearch()方法,以及一个匿名实现内部OnClickListener的Dialog,但都具有相同的结果.我还尝试将孩子们分成独立的Views并以编程方式调用它们:
TextView text = (TextView) findResourceById(R.id.new_independant_textview);
Run Code Online (Sandbox Code Playgroud)
但是,同样的结果.
这是相关代码:
public class Xyz extends Activity {
public void onCreate(...) { // some listener will trigger initSearch() }
private void initSearch() {
AlertDialog.Builder searchDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
searchDialog.setTitle("Search Photos");
searchDialog.setMessage("Specify tag and value...");
// R.layout.search_dialog is my custom layour, it displays fine, it works.
searchDialog.setView(inflater.inflate(R.layout.search_dialog, null));
EditText …Run Code Online (Sandbox Code Playgroud) android nullpointerexception android-dialog findviewbyid android-resources
我需要将几个标签关联到一个视图,所以我使用
view.setTag(id, tag_object)
Run Code Online (Sandbox Code Playgroud)
不幸的是,Android需要具有资源中定义的id.但是R文件是自动生成的资源ID出现在不同的资源文件中,所以我不知道如何创建从任何资源分离的id.作为解决方法我只使用某些资源的id,但它不健壮,因为如果我决定删除资源,id就会消失.它还降低了代码的可读性,该代码具有一些用于寻址标记的奇怪id.也许我错过了非常简单的技巧作为ids资源文件.
每次应用程序渲染一些布局时,我都会在logcat上收到这些警告.试图搜索但无法找到线索.
我认为是与资源(drawables,字符串或值)相关的东西,但我不知道是什么导致了这一点.
这不是错误,但有点烦人.
有人有想法吗?
04-22 15:28:33.204 21943-21943/xx.xxx.xxxxxxx.xxxxx.xxxxx W/ResourceType? For resource 0x01030128, entry index(296) is beyond type entryCount(1)
04-22 15:28:33.204 21943-21943/xx.xxx.xxxxxxx.xxxxx.xxxxx W/ResourceType? For resource 0x01030224, entry index(548) is beyond type entryCount(1)
04-22 15:28:33.204 21943-21943/xx.xxx.xxxxxxx.xxxxx.xxxxx W/ResourceType? For resource 0x01030005, entry index(5) is beyond type entryCount(1)
04-22 15:28:33.204 21943-21943/xx.xxx.xxxxxxx.xxxxx.xxxxx W/ResourceType? For resource 0x01030237, entry index(567) is beyond type entryCount(1)
04-22 15:28:33.204 21943-21943/xx.xxx.xxxxxxx.xxxxx.xxxxx W/ResourceType? For resource 0x0103000c, entry index(12) is beyond type entryCount(1)
04-22 15:28:33.205 21943-21943/xx.xxx.xxxxxxx.xxxxx.xxxxx W/ResourceType? For resource 0x010301ed, entry index(493) is beyond …Run Code Online (Sandbox Code Playgroud) 我有兴趣在strings.xml文件而不是build.gradle中更多地定义我的应用程序.鉴于多种口味,我想要一个简化的发布/调试变体:
buildTypes {
release {
signingConfig signingConfigs.release
resValue "string", "app_name", "@string/prod_name"
}
debug {
applicationIdSuffix ".beta"
resValue "string", "app_name", "@string/beta_name"
}
Run Code Online (Sandbox Code Playgroud)
然后在我的每个构建版本的自定义res/values/strings.xml文件中,我将定义每个自己的"prod_name"和"beta_name".我也想使用这个类似的框架来定义提供者的权限等...
目前这将通过gradle命令行构建良好,但Android Studio无法识别.
Android Studio错误:
我在'generated.xml'中找到了这个
<!-- Values from build type: debug -->
<string name="app_name">@string/beta_name</string>
Run Code Online (Sandbox Code Playgroud)
这是典型的一个字符串如何引用另一个字符串 但是这次Android Studio给了我这个错误:
Error:(7, 29) No resource found that matches the given name
(at 'app_name' with value '@string/beta_name').
Run Code Online (Sandbox Code Playgroud)
我正在使用Android Studio 2.1 Preview 5
android gradle android-resources build.gradle android-gradle-plugin
我想将layout xml文件添加到我的androidTest文件夹中,仅用于测试.
我添加了res/layout文件夹androidTest并尝试向其添加布局文件.但它给错误URI is not registered的xmlns:android="http://schemas.android.com/apk/res/android"
不知何故,该项目不会将其识别为有效的布局文件.
切换到Android Studio 3.2 canary后,我收到以下构建错误.
我看过这个帖子,那个人有类似的问题,但没有提到他们的解决方案.
我想尝试新的材料组件和喷气背包,所以有可能我不需要换回.
com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed
Output: error: resource style/TextAppearance.Compat.Notification.Info (aka com.nsnik.nrs.kotlintest.debug:style/TextAppearance.Compat.Notification.Info) not found.
error: resource style/TextAppearance.Compat.Notification.Info (aka com.nsnik.nrs.kotlintest.debug:style/TextAppearance.Compat.Notification.Info) not found.
error: resource style/TextAppearance.Compat.Notification.Info (aka com.nsnik.nrs.kotlintest.debug:style/TextAppearance.Compat.Notification.Info) not found.
error: resource style/TextAppearance.Compat.Notification (aka com.nsnik.nrs.kotlintest.debug:style/TextAppearance.Compat.Notification) not found.
error: resource style/TextAppearance.Compat.Notification (aka com.nsnik.nrs.kotlintest.debug:style/TextAppearance.Compat.Notification) not found.
error: resource style/TextAppearance.Compat.Notification (aka com.nsnik.nrs.kotlintest.debug:style/TextAppearance.Compat.Notification) not found.
error: resource style/TextAppearance.Compat.Notification.Time (aka com.nsnik.nrs.kotlintest.debug:style/TextAppearance.Compat.Notification.Time) not found.
error: resource style/TextAppearance.Compat.Notification.Time (aka com.nsnik.nrs.kotlintest.debug:style/TextAppearance.Compat.Notification.Time) not found.
error: resource style/TextAppearance.Compat.Notification.Time (aka com.nsnik.nrs.kotlintest.debug:style/TextAppearance.Compat.Notification.Time) not found.
error: resource …Run Code Online (Sandbox Code Playgroud) android android-custom-view android-notifications android-resources android-styles
我想用Proguard我的Android项目和一些额外的Android库项目进行编译.实际上,大多数代码和资源都存在于这些库中.
我在主项目的project.properties中添加了行"proguard.config = proguard.cfg",我已经配置了cfg-file(-libraryjars ..\libs).然后 - 右键单击,Android工具 - >导出签名的应用程序包.ADT编译项目发布,但应用程序在第一次启动时崩溃:当我从日志中读取时,这是因为错误的资源指针.
我已反编译项目并发现非常奇怪的事情:如果我在库中声明资源,那么它上面的资源指针变为0.
当我将"proguard.config = proguard.cfg"行添加到我所有Android库项目的project.properties中时,问题并未消失.
有解决方案吗
编辑:查看proguard.cfg的片段:
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !
-printusage unused.txt
-optimizationpasses 2
-printmapping mapping.txt
# -overloadaggressively
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-libraryjars ..\libs
Run Code Online (Sandbox Code Playgroud) 我正在Android Studio中创建一个Android应用.当我打开布局编辑器时,我看到布局顶部的灰色"弹出窗口",它表示它
无法解析资源@style/Widget.Holo.Light.ActionMode.Inverse.
当我将"渲染版本"更改为API 15(Android 4.0.3)时会发生这种情况.
它在一分钟前工作,但当我切换回布局时,我才得到这个错误.
编辑:我使用的是AppCompat库.
主题定义如下
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar" />
Run Code Online (Sandbox Code Playgroud)
该应用程序在我运行时工作正常,但在编辑器中无法正确呈现(至少它给出了错误).
如果有人能帮助我,我会很高兴的.谢谢!
android rendering android-appcompat android-resources android-actionmode
android ×10
build.gradle ×1
findviewbyid ×1
gradle ×1
logcat ×1
proguard ×1
rendering ×1
resources ×1
warnings ×1