我为我们在整个应用程序中广泛使用的控件编写了一个自定义小部件.widget类ImageButton
以几种简单的方式派生和扩展它.我已经定义了一个可以应用于小部件的样式,但是我更喜欢通过主题来设置它.在R.styleable
我看到小部件样式属性,如imageButtonStyle
和textViewStyle
.有没有办法为我写的自定义小部件创建类似的东西?
我有一个枚举,我需要将值显示为本地化字符串.我目前的做法是:
public enum MyEnum {
VALUE1(R.string.VALUE1),
VALUE2(R.string.VALUE2),
.
.
VALUE10(R.string.VALUE10);
private int mResId = -1;
private MuEnum(int resId) {
mResId = resId;
}
public String toLocalizedString(Resources r) {
if (-1 != mResId) return (r.getString(mResId));
return (this.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法来做到这一点?如果我能以某种方式根据枚举值名称(即'VALUE1')查找资源,我会喜欢它.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="VALUE1"/>My string</string>
<string name="VALUE2"/>My string 2</string>
.
.
<string name="VALUE10"/>My string 3</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
编辑:为了将来参考,这是最适合我的解决方案:
public enum MyEnum {
VALUE1,
VALUE2,
.
.
VALUE10;
/**
* Returns a localized label used to represent this enumeration value. …
Run Code Online (Sandbox Code Playgroud) 是否有可能跨APK共享资源?例如,应用程序A(在APK A中)可以从应用程序B加载图标或布局视图(在APK B中)吗?
一般来说,Eclipse Android插件正在按预期工作,具有自动完成功能.但是,它不适用于我编写的自定义控件.基本上,除默认命名空间选项外,自动完成列表为空.
控件继承自Button小部件并添加一些其他文本.我有一些额外的属性,我使用这些属性定义如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SubTextButton"
<attr name="sub_text" format="string" />
<attr name="sub_text_size" format="float" />
<attr name="sub_text_color" format="color" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
否则它使用Button小部件的所有标准属性.
在布局文件中,我指定了命名空间:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.mycompany"
...
Run Code Online (Sandbox Code Playgroud)
我使用控件:
<com.mycompany.SubTextButton
android:layout_width="@dimen/status_bar_button_w"
android:layout_height="@dimen/status_bar_button_h"
android:layout_alignParentRight="true"
android:background="@drawable/button_bg"
android:text="HCD"
android:textColor="@color/static_text"
android:textSize="@dimen/font_size_standard"
app:sub_text="SET"
app:sub_text_size="12.0"
/>
Run Code Online (Sandbox Code Playgroud)
一切正常,但用户没有自动完成的好处,看看有哪些属性可用.理想情况下,我希望看到继承的Button
属性以及我定义的自定义属性.
有任何想法吗?
我和我的同事并行开发了两个应用程序,每个应用程序都有类似的样式.这些应用程序的主视图背景是径向渐变.他实现了他的九个补丁图像,我做了一个形状drawable.两者都产生类似且可接受的结果
所以我的问题是,我们应该使用哪个?内存消耗和性能之间是否需要权衡?我想图像可能需要一些时间来加载,但是可绘制的形状需要更多的时间来绘制(由于计算).然后将它们存储在缓存中,这些处罚只会在它们第一次显示时发生,或者这些问题是否正在进行中?
形状可绘:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#003472"
android:endColor="#000034"
android:gradientRadius="350"
android:type="radial"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
九补丁:
我已经定义了一些样式资源,包括TextAppearance和定义的TextColor.然后我将样式应用于一些TextViews和Buttons.所有样式都使用TextView,但不是Button.由于某种原因,textColor属性未显示.这是一个错误,还是我在Button的情况下遗漏了什么?
这是样式定义:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="TestApp">
</style>
<!-- Text Appearances -->
<style name="TestApp.TextAppearance">
<item name="android:typeface">sans</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16px</item>
<item name="android:textColor">#6666FF</item>
</style>
<!-- Widget Styles -->
<style name="TestApp.Widget">
<item name="android:layout_margin">3sp</item>
</style>
<style name="TestApp.Widget.Label">
<item name="android:textAppearance">@style/TestApp.TextAppearance</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="TestApp.Widget.Switch">
<item name="android:textAppearance">@style/TestApp.TextAppearance</item>
<item name="android:layout_width">100px</item>
<item name="android:layout_height">100px</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是我尝试应用它们的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
style="@style/TestApp.Widget.Label"
android:text="This is my label." />
<TextView
style="@style/TestApp.Widget.Label"
android:text="This is my disabled label."
android:enabled="false" />
<Button
style="@style/TestApp.Widget.Switch" …
Run Code Online (Sandbox Code Playgroud) 本着重用代码的精神,我正在尝试创建一些库项目。但是,我似乎在定义跨库的 .aidl 文件时遇到了问题。问题是这样的:
AI 库中有 Foo.java 和 Foo.aidl。Foo.java 是 Parcelable 所以 aidl 声明是:
Foo.aidl:
package com.example.library.A;
parcelable Foo;
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试创建库 B。在库 BI 中想要定义一个使用类 Foo 的服务接口:
IMyService.aidl:
package com.example.library.B;
import com.example.library.A.Foo;
interface IMyService {
void requestSomething(in Foo fooBug);
}
Run Code Online (Sandbox Code Playgroud)
该文件无法编译,并抱怨找不到 Foo 的导入。我尝试过引用库 A,并且尝试将库项目添加为外部 jar,但都不起作用。
是否存在我不知道的限制?我的项目设置有问题吗?
我可能应该提到,我已经在项目中直接使用了库 A,没有任何问题,所以我确信库 A 不是问题。