标签: android-attributes

如何在Android中创建AttributeSet?

我试图在Android中编写一些代码来设置AttributeSetfrom attrs.xml文件中的参数.但我收到"资源未找到"错误.

Java代码

MainActivity.java

package com.example.mycompoundbutton;

import org.xmlpull.v1.XmlPullParser;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Xml;
import android.app.Activity;
import android.content.res.Resources;


public class MainActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Resources res = this.getResources();

    XmlPullParser parser = res.getXml(R.attr.xyz);

    AttributeSet attrs = Xml.asAttributeSet(parser);

    MyCompound my = new MyCompound(this,attrs);

    my.MyTestFun(300,500);

}
}
Run Code Online (Sandbox Code Playgroud)

MyCompound.java

package com.example.mycompoundbutton;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.CompoundButton;

public class MyCompound extends CompoundButton
{

public MyCompound(Context context, AttributeSet attrs) 
{

        super(context, attrs);

        TypedArray …
Run Code Online (Sandbox Code Playgroud)

xml android xmlpullparser android-attributes

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

添加 ?attr/selectableItemBackground 到 View 并设置背景颜色

我有一个View以编程方式创建的,我希望在选择它时产生连锁反应。我能够使用?attr/selectableItemBackground. 但是,我还想设置View选择它时的背景颜色。我试过setBackgroundResource(selectableAttr)然后setBackgroundColor(colorSelectBackground),但颜色似乎覆盖了资源,所以我只有一个。这是我的代码:

int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = context.obtainStyledAttributes(attrs);
int backRes = typedArray.getResourceId(0, 0);

public void select() {
    view.setSelected(true);
    view.setBackgroundResource(backRes);
    view.setBackground(colorSelectBackground);
}

public void deselect() {
    view.setSelected(false);
    view.setBackground(colorSelectBackground);
}
Run Code Online (Sandbox Code Playgroud)

任何人都知道我如何使用两者?attr/selectableItemBackground并设置背景颜色?谢谢!

编辑: 澄清一下,有问题的视图不是按钮,而是RelativeLayout.

更新: 我从来没有真正找到一个好的解决方案。我得到的最接近的是使用View.setForeground()到 a Drawablefrom the TypedArray,即

view.setForeground(typedArray.getDrawable(0));
Run Code Online (Sandbox Code Playgroud)

这样做的主要缺点是它仅适用于 API 23+。如果您想出更好的解决方案,请告诉我。

android android-view android-drawable android-attributes

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

Custom attribute not resolved inside styles and theme

I have android application with custom themes which was developed 2-3 year ago. I had this stylable in my attr.xml resouce file:

<declare-styleable name="EVETextViewPlus">
    <attr name="customFont" format="string" />
    <attr name="pageStripFont" format="string" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

And I've used this inside my style_warm.xml resource file:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android"
    <style name="style_display_page_title_warm">
        <item name="my.app.package.:pageStripFont">fonts/b_titr.ttf</item>
        <item name="android:textSize">25dp</item>
        <item name="android:textColor">@color/colorDisplayPageTitle</item>
    </style>
....
Run Code Online (Sandbox Code Playgroud)

When I'm going to sign my app, it checks for errors and prompts:

Cannot resolve symbol 'my.app.package:pageStripFont'

I should mention my.app.package is package …

android gradle android-theme android-styles android-attributes

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

上下文/覆盖主题颜色

我正面临一个问题,我尝试了几种方法来面对它,但仍然不成功.

我的应用程序使用多个主题,如:万圣节,圣诞节等,我正在使用TabLayout背景,文本颜色等小部件上的一些颜色属性来上传应用程序.

问题是:如何根据主题上下文使用具有不同值的相同颜色属性?

所以,基本上这是声明颜色的常规方法:

<color name="mapMarkerSelectedTextColor">@android:color/white</color>
<color name="mapLoadingIndicatorColor">@color/white</color>
Run Code Online (Sandbox Code Playgroud)

但是,主题和颜色是不可变的,所以我想,也许我可以覆盖每个主题内的那些颜色,如:

    <item name="mapMarkerUnselectedTextColor">@color/christmas_red</item>
    <item name="mapMarkerSelectedTextColor">@color/white</item>
Run Code Online (Sandbox Code Playgroud)

=>不成功

其他线索,将这些颜色声明为属性:

<attr name="mapLoadingIndicatorColor" format="reference|color" />
<attr name="map_autocomplete_accent_color" format="reference|color" />
Run Code Online (Sandbox Code Playgroud)

并在我的XML中使用主题:" ?attr/mapLoadingIndicatorColor".但是这个功能只允许自Lollipop版本以来导致崩溃.

我已经阅读了很多关于主题定制,颜色覆盖,但从未找到关于这种情况的明确解决方案.

不管怎么说,还是要谢谢你.

android colors android-theme android-styles android-attributes

5
推荐指数
1
解决办法
817
查看次数

通过数据绑定 API 设置一个众所周知的属性值

我有一个来自自定义视图定义行的自定义属性:

<declare-styleable name="ExampleView">
    <attr name="order">
        <enum name="byValue" value="0" />
        <enum name="byKey" value="1" />
    </attr>
    <!-- and some more attributes -->
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

Android Studio 检测到这一点并为我提供自动完成功能,这很棒。所以 xml 属性看起来像app:order="byValue". 但是,由于我想使用BindingAdapter数据绑定 API 中的 a,因此我需要将它与这样的@符号一起使用:app:order="@{byValue}",不幸的是这不能编译。

然后我尝试使用我在内部使用的常量,如下所示:app:order="@{com.example.views.ExampleView.ORDER_BY_VALUE}",但这也不能编译。我可以只使用app:order="@{0}",确保这有效,因为它是这样定义的,但是为什么我在0那里使用它并不直观。

知道如何编写更具可读性的代码来解决此问题吗?

android android-attributes android-databinding android-binding-adapter

5
推荐指数
1
解决办法
384
查看次数

在自定义视图中重用 TextView 中的“inputType”属性

我试图在自定义视图中重用 TextView 中的“android:inputType”属性,但得到的错误是:

不允许使用字符串类型(在“inputType”处,值为“textMultiline”)。

我已参考/sf/answers/725313221/的解决方案

attr 文件包含:

<declare-styleable name="MyEditText">
    <attr name="android:inputType"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

MyEditText.java 是:

int n = typedArray.getIndexCount();
for (int i = 0; i < n; i++) {
    int attr = typedArray.getIndex(i);

    switch (attr) {
        case R.styleable.MyEditText_android_inputType:
            inputTypes = typedArray.getInt(attr, EditorInfo.TYPE_NULL);
            break;

    }
}
Run Code Online (Sandbox Code Playgroud)

布局文件包含:

<com.example.MyEditText
  android:id="@+id/met"
  style="@style/MyStyle"
  android:layout_marginLeft="0dp"
  android:layout_marginRight="0dp"
  android:layout_marginTop="12dp"
  android:inputType="textMultiline" />
Run Code Online (Sandbox Code Playgroud)

我有什么办法可以解决这个问题吗?谢谢。

android textview android-custom-view android-edittext android-attributes

5
推荐指数
1
解决办法
3068
查看次数

具有自定义背景和“?attr / selectableItemBackground”的按钮

如何定义Button具有自定义背景和属性的
"?attr/selectableItemBackground"

使用此代码,该"?attr/selectableItemBackground"属性将被忽略,它不会显示触摸反馈。

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:foreground="?attr/selectableItemBackground"/>
Run Code Online (Sandbox Code Playgroud)

使用此其他代码,可选择的作品,但我失去了背景色:

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"/>
Run Code Online (Sandbox Code Playgroud)

android android-button android-view material-design android-attributes

5
推荐指数
1
解决办法
3039
查看次数

如何在自定义视图中获取android默认属性

我创建了一个简单的自定义视图,其中包含a RelativeLayoutEditText:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edt_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

此外,我添加了一些自定义属性res/values/attrs.xml,我在自定义视图构造函数中检索这些属性,一切正常.

现在,我想EditText在自定义视图中检索默认属性,例如我想android:text在自定义视图中获取属性.

我的自定义视图类(简化):

public class CustomEditText extends RelativeLayout {
    private int panCount;

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
            R.styleable.CustomEditText, 0, 0);
        try {
            this.panCount = typedArray.getInt(R.styleable.CustomEditText_panCount, 16);
        } finally {
            typedArray.recycle();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果不重新声明文本属性,我怎么能这样做res/values/attrs.xml呢?

android android-widget custom-view android-custom-view android-attributes

5
推荐指数
1
解决办法
5108
查看次数

在属性的格式中(以颜色为例),“颜色”和“颜色|参考”之间有什么区别?

TLDR 版本

我明白为什么reference在设置默认主题时用作指向默认样式的属性的格式,但是 using与定义颜色属性时reference|color仅使用有何不同?color您已经可以使用@color/xxx它已经是对另一个资源的引用,那么引用是隐式的吗?如果不是,那么其中一个的用例是什么?

完整版

我一直在遵循使用以下推荐技术对应用程序的自定义小部件进行主题化的最佳实践。

在 attrs.xml 中

<!-- Attributes holding default styles -->
<attr name="myWidgetStyle"      format="reference" />
<attr name="myOtherWidgetStyle" format="reference" />

<!-- Custom attributes -->
<attr name="indicatorColor" format="color|reference" />

<!-- Assigning attributes to controls -->
<declare-styleable name="MyWidget">
    <item name="android:text" />
    <item name="indicatorColor" />
</declare-styleable>

<declare-styleable name="MyOtherWidget">
    <item name="android:text" />
    <item name="indicatorColor" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

在 styles.xml 中

<style name="ThemeBase">

    <!-- Store default style in the style-reference attributes -->
    <item name="myWidgetStyle">@style/MyWidget</item>
    <item …
Run Code Online (Sandbox Code Playgroud)

android android-theme android-styles android-attributes

5
推荐指数
1
解决办法
2191
查看次数

如何避免 CustomView 中资源“attr/*”的重复值

如果我导入:

CustomViewA(从Maven导入)

<declare-styleable name="CustomViewA">
        <attr name="min" format="float"/>
        <attr name="max" format="float"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

CustomViewB(从Maven导入)

<declare-styleable name="CustomViewB">
        <attr name="min" format="float"/>
        <attr name="max" format="float"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

这将失败,说明minmax重复。我以为 Android 会通过 来区分declare-styleable name,但我猜不会。话虽如此,命名自定义视图attr以避免将来可能出现的重复值冲突的最佳方法是什么?

到目前为止我得到的唯一解决方案是:

<attr name="minForMyCustomViewHopingNoOneUsesThisName" format="float"/>
Run Code Online (Sandbox Code Playgroud)

这很糟糕。

android android-custom-view android-styles android-attributes

5
推荐指数
2
解决办法
1177
查看次数