相关疑难解决方法(0)

如何获取在代码中的attrs.xml中创建的枚举

我创建了一个自定义视图(在此处找到),其中包含enum类型的declare-styleable属性.在xml中,我现在可以为自定义属性选择一个枚举条目.现在我想创建一个以编程方式设置此值的方法,但我无法访问枚举.

attr.xml

<declare-styleable name="IconView">
    <attr name="icon" format="enum">
        <enum name="enum_name_one" value="0"/>
        ....
        <enum name="enum_name_n" value="666"/>
   </attr>
</declare-styleable>     
Run Code Online (Sandbox Code Playgroud)

layout.xml

<com.xyz.views.IconView
    android:id="@+id/heart_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:icon="enum_name_x"/>
Run Code Online (Sandbox Code Playgroud)

我需要的是:mCustomView.setIcon(R.id.enum_name_x); 但是我找不到枚举,或者我甚至不知道如何获得枚举或枚举的名称.

enums android custom-view android-custom-view attr

95
推荐指数
5
解决办法
4万
查看次数

如何在android中以编程方式创建视图时传递AttributeSet

我以编程方式创建像horizo​​ntalview那样,如何以编程方式传递AttributeSet.

我的构造函数看起来像这样:

public HorizontalListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
Run Code Online (Sandbox Code Playgroud)

我试过这个:

mHlvSimpleList= new HorizontalListView(mcontext,R.style.niceview);
Run Code Online (Sandbox Code Playgroud)

错误:

构造函数Horizo​​ntalListView(Context,int)未定义

在style.xml中

<style name="niceview">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>

</style>
Run Code Online (Sandbox Code Playgroud)

如何在horizo​​ntalistview构造函数匹配参数中传递AttributeSet?

android android-styles

36
推荐指数
2
解决办法
4万
查看次数

如何以编程方式创建Android主题样式?

有很多关于通过XML创建或自定义Android主题样式的文档和教程,但是还没有找到在代码中创建它的方法.关于如何在代码而不是xml中创建样式的任何想法?

这是示例XML,需要在代码中以编程方式创建:

<resources>
  <style name="AppTheme" parent="android:Theme.Material">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <item name="android:colorAccent">@color/accent</item>
  </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

android android-styles

23
推荐指数
2
解决办法
7903
查看次数

尝试在代码中获取属性值会返回不正确的值

我希望从样式资源中提取多个属性(仅对属于TextAppearance组的属性感兴趣)

风格定义如此

<style name="Label" parent="@android:style/TextAppearance.Small">
    <item name="android:textColor">@color/floatlabel_text</item>
    <item name="android:textSize">8dp</item>
    <item name="android:textStyle">bold</item>
</style>
Run Code Online (Sandbox Code Playgroud)

第一次尝试

首先我尝试了TextView(第663-731行)如何实现它,但后来我发现我们无法访问com.android.internal.R

部分解决方案

这就是我切换到这个解决方案的原因:https://stackoverflow.com/a/7913610/3922891

所以我创建了textAppearanceAttr来替换com.android.internal.R.styleable.TextAppearance(只包含我感兴趣的10/13 TextAppearance属性)

int[] textAppearanceAttr = new int[]{    
        android.R.attr.textColor,
        android.R.attr.textSize,
        android.R.attr.typeface,
        android.R.attr.fontFamily,
        android.R.attr.textStyle,
        android.R.attr.textAllCaps,
        android.R.attr.shadowColor,
        android.R.attr.shadowDx,
        android.R.attr.shadowDy,
        android.R.attr.shadowRadius};
Run Code Online (Sandbox Code Playgroud)

这是我如何使用它.我得到了样式的资源ID(资源由clTextAppearance属性引用)

   int ap = a.getResourceId(R.styleable.CustomLabelLayout_clTextAppearance, android.R.style.TextAppearance_Small);
   TypedArray appearance = mContext.obtainStyledAttributes(ap, textAppearanceAttr);
Run Code Online (Sandbox Code Playgroud)

以下是我获取属性的方法(仍然按照上面的链接回答):

    mLabelTextColor = appearance.getColorStateList(0);
    mLabelTextSize = appearance.getDimensionPixelSize(1, 15);
    mLabelTypeface = appearance.getInt(2, -1);
    mLabelFontFamily = appearance.getString(3);
    mLabelTextStyle = appearance.getInt(4, -1);
    (5 more...)
Run Code Online (Sandbox Code Playgroud)

目前的问题

似乎只有第一个属性被设置,其他每个属性都设置为默认值或null.

一个似乎有效的黑客攻击

个别数组:

int[] textSizeAttr = new int[] …
Run Code Online (Sandbox Code Playgroud)

android android-layout

14
推荐指数
3
解决办法
5060
查看次数

使用@StyleableRes注释的obtainStyledAttributes.抑制警告

在看这里的答案时,我遇到了问题:

TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, attrs);
Run Code Online (Sandbox Code Playgroud)

似乎Android Studio不允许我传入一个不是来自R.styleable资源而没有获得警告的int .

它告诉我它需要一个类型为styleable的资源,我假设这意味着我调用的代码已经使用@StyleableRes注释进行了注释

类型风格的预期资源

阅读R.style {x}中定义的值的最佳行动方案是什么?链接帖子上接受的答案有效并编译,但我不知道如何压制警告.因为有警告,压制是否安全?如果是这样,怎么样?

java android annotations android-resources android-studio

6
推荐指数
1
解决办法
3824
查看次数

在Androidstyles.xml 中,如何从我的自定义样式中引用另一个样式属性?

我在styles.xml 中定义了以下样式:

<style name="NotificationTitle"
    parent="android:TextAppearance.Material.Notification.Title" />
Run Code Online (Sandbox Code Playgroud)

如您所见,它扩展了 Android 中定义的以下默认样式:

<style name="TextAppearance.Material.Notification.Title">
    <item name="textColor">@color/notification_primary_text_color_light</item>
    <item name="textSize">@dimen/notification_title_text_size</item>
</style>
Run Code Online (Sandbox Code Playgroud)

现在,回到我的styles.xml 文件,我想创建以下自定义样式,其“android:tint”属性设置为与父样式中定义的“textColor”相同的值。我试过这样的事情:

<style name="NotificationIcon" >
    <item name="android:tint">@style/NotificationTitle.textColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)

但是我不能像那样引用“textColor”,它无法解析符号。那么如何从我的自定义样式中引用另一种样式中定义的另一个属性呢?如果不可能,最好的选择是什么?

android android-layout android-styles

5
推荐指数
0
解决办法
802
查看次数