我在/res/values/colors.xml下的Android应用程序中创建了一个colors.xml文件.内容是......
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Green">#00ff00</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
我尝试使用...更新我的TableRow的背景
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(R.color.Green);
Run Code Online (Sandbox Code Playgroud)
这不会将其设置为绿色,而是灰色.无论我添加到colors.xml文件的值是什么,它总是相同的灰色.不过这确实有用......
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(android.graphics.Color.GREEN);
Run Code Online (Sandbox Code Playgroud)
我的colors.xml有问题吗?
任何人都知道我们需要在RES目录中使用(或建议使用)标准前缀列表.即Android推荐ic_ for icons,这很棒,我正在关注它.
但是Logos,按钮图像,导航图像等呢?
我真的很想知道其他人是否存在以及其他人正在使用什么
提前致谢
我有一个Android应用程序,我想有不同的口味.具体来说,我想有2种口味,每种口味使用不同的字符串(不同的strings.xml文件),也许有一些不同的图标.
我曾尝试创建项目的根文件夹两个文件夹:flav1和flav2并用以下build.gradle
android {
compileSdkVersion "Google Inc.:Google APIs:15"
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
productFlavors {
flav1 {
packageName "com.ic.flav1"
}
flav2 {
packageName "com.ic.flav2"
}
}
android.sourceSets.flav2 {
res {
srcDir 'flav2'
}
resources {
srcDir 'flav2'
}
}
android.sourceSets.flav1 {
res {
srcDir 'flav1'
}
resources {
srcDir …Run Code Online (Sandbox Code Playgroud) 我为7"平板电脑准备了我的抽屉(Nexus 7)
drawable-large-hdpi-port
layout-large-hdpi-port
Run Code Online (Sandbox Code Playgroud)
这样做时我遇到了错误.我不明白我做错了什么.
我想为7",10"平板电脑准备横向和纵向的布局
在Android Studio中,可以有不同的生成类型,每一种都有自己的配置,类似于产品的味道(如图所示这里)
我希望每次我的应用程序安装在某个地方时,我都会立即知道它的类型 - 发布或调试,只需查看它即可.
为此,我想我可以使用build.gradle文件:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
Run Code Online (Sandbox Code Playgroud)
事情是,我不知道该放什么.我希望应用程序名称不同(并且在字符串文件中有字符串,因为它已被翻译),并且我想将应用程序中某些内容的样式设置为不同(例如,操作栏的颜色) .
我发现我可以使用"resValue"(在这里找到它),但由于某种原因,无论我做什么,它都无法编译:
如何为构建类型使用不同的资源值,即使它们已经存在?
android android-resources android-studio android-productflavors android-build-type
似乎新的百分比支持库已发布,但不允许在维度xml文件中引用百分比值.
也就是说,而不是:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentFrameLayout/>
Run Code Online (Sandbox Code Playgroud)
能够编写类似的代码:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
app:layout_widthPercent="@percent/myWidthPercent"
app:layout_heightPercent="@percent/my/HeightPercent"
app:layout_marginTopPercent="@percent/myMarginTophPercent"
app:layout_marginLeftPercent="@percent/myMarginLeftPercent"/>
Run Code Online (Sandbox Code Playgroud)
其中myWidthPercent在资源文件中定义.
我错了(我是否错过了另一个名字)或者是我们可以发送给谷歌的功能请求?
Android崩溃通常是由包含%1$ s何时%1$s打算的资源引起的.
令我惊讶的是,Android Studio没有显示以下语法错误:
↑上面的真正语法错误是%1$ s(使应用程序崩溃),而不是Android Studio突出显示的省略号.
如何检查Android Studio中的资源变量拼写错误?
我正在寻找相当于lint --check StringFormatInvalid.
上下文:许多半自动语言翻译工具会破坏变量,使应用程序崩溃.
我想了解如何更好地在 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
如果我定义为一个可绘制density qualified folder(例如,绘制,华电国际),也可绘依傍的drawable-nodpi,将一个high density device使用-hdpi过-nodpi?
如果我更进一步,并为-land文件夹设置相同的设置呢?
我想从项目中删除未使用的资源以减少应用程序的大小.有效地使用Android Studio IDE有没有办法做到这一点?