我想在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()全局变量之前使用资源?
我有一个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,所以我该怎么做呢?提前致谢
我有一个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.我怎样才能解决这个问题?
我在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?
当然我可以右键单击原始文件夹并添加新文件夹,但是当我导出项目时,APK文件不包含新的子文件夹.
我尝试运行我的项目时收到此错误.我已经安装了最新的构建工具-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) 我有一个带有自定义适配器的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 O引入了一项新功能,即XML字体,可让您将字体用作资源.我font在Android开发人员提供的资源文件中创建文件夹,但问题是我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)
请检查font我res用Montserrat_Regular.otf文件创建的文件夹
使用上面的内容时出现以下错误,如下所示:
我搜索了它但没有得到预期的结果,请检查:
并 Font resource file没有创建,因为我点击了Right-click the font folder and go to New > Font resource file.但是没有获得选项Font resource file,请查看以下内容:
一位 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) 我想了解如何更好地在 Compose 项目中使用 dp 和 sp 值。我检查了几个开源 Compose 项目,其中大多数都对维度进行了硬编码。这绝对不是支持灵活性和不同屏幕尺寸的方法。我目前看到几个方法:
dimens.xml直接在调用 的 compose 函数中获取值dimensionResource()。dimens.xmlKotlin 类中 的值。例如:
class AppDimensions {
val paddingSmall: Dp
@Composable
get() = dimensionResource(R.dimen.padding_small)
...
}
Run Code Online (Sandbox Code Playgroud)
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
android ×10
build.gradle ×1
dimension ×1
fonts ×1
imageview ×1
java ×1
layout ×1
listview ×1
screen-size ×1
static ×1
string ×1
unit-testing ×1