标签: android-resources

如何在onCreate()之前对静态String使用getString()?

我想在getString()创建活动之前使用从资源中获取String以将其分配给String数组:

private static final String[] MenuNames = {
    Resources.getSystem().getString(R.string.LCMeterMenu),
    Resources.getSystem().getString(R.string.FrecMenu),
    Resources.getSystem().getString(R.string.LogicAnalyzerMenu),
    "Prueba con achartengine",
    Resources.getSystem().getString(R.string.BrazoMenu)
};
Run Code Online (Sandbox Code Playgroud)

当我使用时Resources.getSystem().getString(R.string.LCMeterMenu),Eclipse不会抱怨但是我在运行时遇到错误:

引起:android.content.res.Resources $ NotFoundException:字符串资源ID#0x7f0a000a

但如果我把它放进去onCreate():

Log.i("StringR", "String: " + getString(R.string.LCMeterMenu));
Run Code Online (Sandbox Code Playgroud)

我得到了String,但我不能将它分配给我之前定义的最终字符串.如果我在使用getString()之前只使用onCreate()静态错误消息.如何在onCreate()全局变量之前使用资源?

string static android android-resources

18
推荐指数
1
解决办法
2万
查看次数

代码中的Android ImageView setImageResource

我有一个imageView,我想显示你当前所在国家的一个小图标.我可以获得国家代码,但问题是我无法动态更改imageView资源.我的图片文件都是小写的(例如:国家代码= US,图片文件=我们)

我的代码(countryCode是大写字母的当前countryCode):

    String lowerCountryCode = countryCode.toLowerCase();
    String resource = "R.drawable." + lowerCountryCode;
    img.setImageResource(resource);
Run Code Online (Sandbox Code Playgroud)

现在,当然这不起作用因为setImageResource想要一个int,所以我该怎么做呢?提前致谢

android imageview android-resources

18
推荐指数
2
解决办法
9万
查看次数

Android Studio Unit Test中的Assets文件夹

我有一个Gradle项目,具有以下结构:

project/
    src/
        androidTest/
            java/
        main/
            java/
            res/
            AndroidManifest.xml
    build.gradle
Run Code Online (Sandbox Code Playgroud)

现在我想添加一个使用资源("原始"或"资产")的单元测试.

我把我的资源放入project/androidTest/assets/test_file并使用getContext().getResources().getAssets().open("test_file");(在一个AndroidTestCase)中访问它.

但是,这给了我一个FileNotFoundException.我怎样才能解决这个问题?

java android unit-testing android-resources

18
推荐指数
2
解决办法
5393
查看次数

android屏幕大小,值-v11,值-v14

我在values文件夹中找到了几个文件res夹:

xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp  
res/values-normal/ ==    res/values-v11/ ???
res/values-xlarge/ ==   res/values-v14/  ???
Run Code Online (Sandbox Code Playgroud)

我对吗?或者我需要创建一个名为另一个文件夹values-xlarge,values-normal

android screen-size android-resources

17
推荐指数
2
解决办法
3万
查看次数

如何在我的Android应用程序中将子文件夹添加到原始文件夹?

当然我可以右键单击原始文件夹并添加新文件夹,但是当我导出项目时,APK文件不包含新的子文件夹.

android android-resources

17
推荐指数
1
解决办法
9066
查看次数

即使使用最新的构建工具,任务':app:processDebugResources'的执行也失败了

我尝试运行我的项目时收到此错误.我已经安装了最新的构建工具-23.0.3但仍然存在错误.我该如何解决?

Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources]

Configuration on demand is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2321Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72321Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2321Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72321Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42321Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2321Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing810Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement810Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest
:app:processDebugResources
AGPBI: {"kind":"error","text":"Error retrieving parent for item: No resource found that matches the given name \u0027android:TextAppearance.Material.Widget.Button.Inverse\u0027.","sources":[{"file":"C:\\Users\\Ishmita\\Documents\\GitHub\\FurAlert-Native-Build\\app\\build\\intermediates\\res\\merged\\debug\\values-v23\\values-v23.xml","position":{"startLine":3}}],"original":"","tool":"AAPT"}

AGPBI: {"kind":"error","text":"Error retrieving parent for item: No resource found that matches …
Run Code Online (Sandbox Code Playgroud)

android android-resources android-studio build.gradle

17
推荐指数
3
解决办法
12万
查看次数

ListView的id属性为'android.R.id.list'我正确设置ListView ID时出错

我有一个带有自定义适配器的Listview.我得到了标准

Your content must have a ListView whose id attribute is 'android.R.id.list'
Run Code Online (Sandbox Code Playgroud)

但是,错误,我的ListView有一个android:id="@android:id/list"值集.这真的是我的头脑.任何建议?

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/Layout">
    <ListView android:id="@android:id/list" style="@stlye/ListMenuView"></ListView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

styles.xml

<resources>
    <style name="Layout" parent="@android:Theme">
        <item name="android:background">@drawable/background</item>
        <item name="android:scaleType">fitXY</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:orientation">vertical</item> 
    </style>
    <style name="ListMenuView">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:cacheColorHint">#00000000</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

我的onCreate方法:

/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.main );

    m_menu = new ArrayList<ListItemData>(); …
Run Code Online (Sandbox Code Playgroud)

android listview android-resources

16
推荐指数
2
解决办法
3万
查看次数

在使用Android O的"android:fontFamily"时获取错误"文件名必须以.xml结尾"?

Android O引入了一项新功能,即XML字体,可让您将字体用作资源.我fontAndroid开发人员提供的资源文件中创建文件夹,但问题是我file name must end with .xml在使用fontAndroid O版本中提供的文件夹时收到错误.

请检查以下布局.

 <TextView
   android:id="@+id/txtMsgCount"
   android:layout_width="wrap_content"
   android:layout_height="16dp"
   android:background="@drawable/msg_count"
   android:gravity="center"
   android:text="123"
   android:fontFamily="@font/Montserrat_Regular" ////IT IS MY FONT STYLE
   android:textColor="@android:color/white"
   android:textSize="10sp" />
Run Code Online (Sandbox Code Playgroud)

请检查fontresMontserrat_Regular.otf文件创建的文件夹

我的字体文件夹,字体样式

使用上面的内容时出现以下错误,如下所示:

得到错误

我搜索了它但没有得到预期的结果,请检查:

1.第一链接
2.第二链接
3.第三链接
4.第四链接

Font resource file没有创建,因为我点击了Right-click the font folder and go to New > Font resource file.但是没有获得选项Font resource file,请查看以下内容:

在此输入图像描述

layout fonts android android-resources android-8.0-oreo

16
推荐指数
1
解决办法
2万
查看次数

Android ResourcesNotFoundException 但是图像存在于所有可绘制文件夹中

一位 Android 用户在 Nexus 5X 设备上发生了崩溃,并报告如下:

Fatal Exception: java.lang.RuntimeException
Unable to start activity ComponentInfo{package.MainActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
android.app.ActivityThread.performLaunchActivity
Caused by android.view.InflateException
Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
Caused by android.view.InflateException
Binary XML file line #8: Error inflating class ImageView

Caused by android.content.res.Resources$NotFoundException
Drawable (missing name) with resource ID #0x7f0700b8

Caused by android.content.res.Resources$NotFoundException
Unable to find resource ID #0x7f0700b8
Run Code Online (Sandbox Code Playgroud)

指向Mainactivity中的第61行:MainActivity.onCreate(MainActivity.java:61),也就是这一行:

 this.setContentView(R.layout.activity_main); …
Run Code Online (Sandbox Code Playgroud)

android android-resources

16
推荐指数
0
解决办法
1421
查看次数

在 Jetpack Compose 中使用 dp 和 sp 尺寸的最佳实践是什么?

我想了解如何更好地在 Compose 项目中使用 dp 和 sp 值。我检查了几个开源 Compose 项目,其中大多数都对维度进行了硬编码。这绝对不是支持灵活性和不同屏幕尺寸的方法。我目前看到几个方法:

  1. 使用旧方法并dimens.xml直接在调用 的 compose 函数中获取值dimensionResource()
  2. 引用dimens.xmlKotlin 类中 的值。例如:
    class AppDimensions {
        val paddingSmall: Dp
        @Composable
        get() = dimensionResource(R.dimen.padding_small)
    ...
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 不要针对不同的屏幕尺寸使用dimens.xml并实现自己的逻辑。例如:
    class AppDimensions {
        val paddingSmall = when(screenSize) {
            Compact -> 10.dp
            Medium -> 16.dp
            Expanded -> 20.dp
            else -> 10.dp
        }
        ...
    }
    
    Run Code Online (Sandbox Code Playgroud)

我喜欢第三种变体,因为它看起来更灵活并且允许我们避免返回 XML。但这需要努力来支持。

但是,也许我们可以以更方便的方式使用它吗?

android dimension android-resources android-jetpack android-jetpack-compose

15
推荐指数
1
解决办法
5503
查看次数