标签: android-custom-attributes

自定义视图的attrs.xml中具有相同名称的属性

我正在写一些自定义视图,它们共享一些同名的属性.在他们各自的<declare-styleable>部分,attrs.xml我想对属性使用相同的名称:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView1">
        <attr name="myattr1" format="string" />
        <attr name="myattr2" format="dimension" />
        ...
    </declare-styleable>

    <declare-styleable name="MyView2">
        <attr name="myattr1" format="string" />
        <attr name="myattr2" format="dimension" />
        ...
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,说明myattr1并且myattr2已经定义了.我发现我应该省略formatfor myattr1myattr2in 的属性MyView2,但如果我这样做,我在控制台中获得以下错误:

[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute 
Run Code Online (Sandbox Code Playgroud)

有没有办法可以实现这一点,也许某种命名空间(只是猜测)?

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

166
推荐指数
4
解决办法
3万
查看次数

如何将用户定义的属性/值添加到Android清单文件中?

我想在清单文件中添加自定义属性/属性,并能够在运行时读取它.我想这样做,所以我可以通过这些清单属性自定义应用程序的行为.如何才能做到这一点?

android android-manifest android-custom-attributes

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

以编程方式更改ConstraintLayout子项的边距和大小

我正在编写一个自定义视图,以便能够使用自定义XML属性根据这些属性设置视图的边距和大小,一切正常,直到我到达ConstraintLayout的子项获得其边距和大小由自定义值确定的部分.边距没有任何区别,视图保留在其父ConstraintLayout的左上角.可能是什么问题呢?(图像应距离屏幕边界500PXs)

if (hasCustomWidth || hasCustomHeight || hasCustomTopMargin || hasCustomRightMargin || hasCustomBottomMargin || hasCustomLeftMargin) {
    if(((ViewGroup)getParent()) instanceof  LinearLayout) {
        LinearLayout.LayoutParams newLayoutParams = new LinearLayout.LayoutParams((int) Math.round(customWidth), (int) Math.round(customHeight));
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) newLayoutParams;
        marginLayoutParams.setMargins((int) Math.round(customLeftMargin), (int) Math.round(customTopMargin), (int) Math.round(customRightMargin), (int) Math.round(customBottomMargin));
        setLayoutParams(newLayoutParams);
    } else if(((ViewGroup)getParent()) instanceof ConstraintLayout) {
        ConstraintLayout parentConstraintLayout = (ConstraintLayout)CustomAppCompatImageView.this.getParent();

        ConstraintLayout.LayoutParams newLayoutParams = new ConstraintLayout.LayoutParams((int) Math.round(customWidth), (int) Math.round(customHeight));

        ConstraintSet constraintSet = new ConstraintSet();

        constraintSet.clone(parentConstraintLayout);

        constraintSet.connect(CustomAppCompatImageView.this.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 500);
        constraintSet.setMargin(CustomAppCompatImageView.this.getId(), ConstraintSet.LEFT, 500);

        setLayoutParams(newLayoutParams);
        constraintSet.applyTo(parentConstraintLayout);

        parentConstraintLayout.invalidate();
    } else {
        throw new …
Run Code Online (Sandbox Code Playgroud)

android android-custom-attributes android-constraintlayout

13
推荐指数
1
解决办法
4298
查看次数

Android中的安全枚举自定义属性

我认为这种情况很常见.我有一个Java枚举:

public enum Flavour { CHOCOLATE, STRAWBERRY }
Run Code Online (Sandbox Code Playgroud)

我在attrs.xml中定义了一个自定义属性:

<attr name="flavour">
    <enum name="chocolate" value="0" />
    <enum name="strawberry" value="1" />
</attr>
Run Code Online (Sandbox Code Playgroud)

但这感觉非常脆弱.它依赖于我正确地从属性手动映射到枚举.

如果有人在枚举Flavor的末尾添加"SARDINE",那么它显然不会自动添加到属性定义中.这很公平.

但更糟糕的是,如果有人在枚举的中间添加"SARDINE",它将破坏使用"草莓"的xml布局.

人们如何克服这一点?我考虑使用字符串(和使用Flavour.valueOf()),但我希望那里可能有一个更清洁的解决方案.

android android-custom-attributes

9
推荐指数
1
解决办法
563
查看次数

TypedArray的getResourceId方法

我正在阅读有关getResourceId()方法的文档.它说:

检索索引处属性的资源标识符.请注意,在检索整个TypedArray对象时,属性资源已解析.因此,此函数将返回找到的最终资源值的资源标识符,而不一定是该属性指定的原始资源.

所以

  • 第一段很清楚:

检索索引处属性的资源标识符.

  • 第二个也是明确的:

请注意,在检索整个TypedArray对象时,属性资源已解析.

  • 但3段是什么意思? 为什么它可能 不一定返回原始资源ID

因此,此函数将返回找到的最终资源值的资源标识符,而不一定是该属性指定的原始资源.

android android-custom-view typedarray android-resources android-custom-attributes

9
推荐指数
2
解决办法
1745
查看次数

MotionLayout 中的 Android CustomAttribute 来更改 TextView 中的文本

任何人都可以帮助我以正确的方式更改 中TextView的文本MotionLayout......这就是我所做的。

我测试了MotionLayout一个简单的应用程序......我在到达部分运动教程CustomAttributes

使用它们,您可以更改BackgroundColora 的View,也可以textColor使用customColorValue

在这种情况下,您可以看到它在开始和结束场景中更改这些值非常有效:

        <CustomAttribute
            motion:attributeName="backgroundColor"
            motion:customColorValue="#004A6D" />

        <CustomAttribute
            motion:attributeName="textColor"
            motion:customColorValue="#000000" />
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

另外我注意到有一个customStringValue所以我想我可以将TextView文本更改为“BEFORE”->“AFTER”。但是当我尝试CustomAttribute在应用程序崩溃时设置它时。

在开始场景中:

<CustomAttribute
            motion:attributeName="Text"
            motion:customStringValue="BEFORE" />
Run Code Online (Sandbox Code Playgroud)

在最后的场景中:

        <CustomAttribute
            motion:attributeName="Text"
            motion:customStringValue="AFTER" />
Run Code Online (Sandbox Code Playgroud)

在 MotionScene 之外,textView文本是TEST

  • 当我CustomAttribute为结束场景设置only 时...文本从初始值TEST更改为结束AFTER值......所以它部分工作但永远不会返回到初始状态。
  • 当没有设置初始文本时也会发生这种情况TextView。它部分工作。

在此处输入图片说明

所以......任何人都可以帮我用正确的方式来改变TextView在的文本MotionLayout

android android-custom-attributes android-motionlayout

8
推荐指数
2
解决办法
6738
查看次数

将默认颜色设置为Android库

所以我有一个Android ,我希望其他人能够在使用它时轻松自定义其颜色.问题是颜色不仅仅是一个属性(如视图背景),但它更像是一个主题颜色(视图背景,文本颜色,按钮的笔触等)所以我是无法将其作为视图属性传递.所以我最终使用了a color reference并在样式,布局和drawable中使用它:

colors.xml:

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

layout.xml:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/color">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="?attr/color"/>
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/LibraryTheme.TextAppearance"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

库项目中的styles.xml:

<style name="LibraryTheme.TextAppearance">
    <item name="android:textColor">?attr/color</item>
    <item name="colorControlNormal">?attr/color</item>
    <item name="android:textColorHint">?attr/color</item>
    <item name="colorControlActivated">?attr/color</item>
    <item name="colorControlHighlight">?attr/color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这很有效,当有人使用我的lib时,他必须声明他希望你在他的主题中使用的颜色:

app项目中的styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="color">@color/my_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

问题是这样的:我想用默认的配色方案来提供这个库.它现在可以通过2个选项执行:

  1. 使用默认颜色集声明我自己的主题,用户需要从我的主题继承他的主题.
  2. 在我的color.xml中放置一些default_color,以便用户可以将其用作主题中的颜色.

第一个非常糟糕,因为我不能强迫用户使用特定的应用主题(如AppCompact.Light).第二个也不是那么好,因为我希望用户能够无缝地使用默认值,并且我需要在主题中设置几种颜色.

有没有其他方式你认为我可以让其他用户轻松玩颜色?

谢谢.

android android-custom-view android-theme android-view android-custom-attributes

7
推荐指数
2
解决办法
1920
查看次数

自定义XML属性,没有自定义View in Fragment

我正在尝试在现有View中添加自定义XML属性

将它们添加到自定义视图并不是一件大事,但我不知道如何在"基本"视图中访问这些属性(例如TextView,LinearLayout,ImageView ......)

当涉及片段和/或图书馆项目时,这将更加困难

到目前为止,这是我的代码

海关属性定义和XML(attrs.xml和布局):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="StarWars">
    <attr name="jedi" format="string" />
    <attr name="rank" format="string" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:sw="http://schemas.android.com/apk/res-auto"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dp"
    android:gravity="center_horizontal"
    sw:jedi="Obiwan" />

<TextView
    android:id="@+id/rank"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dp"
    android:gravity="center_horizontal"
    sw:rank="Master" />
Run Code Online (Sandbox Code Playgroud)

因为我在片段上膨胀这个(所以没有onCreate与attrs arg!),尝试获得2个sw自定义属性的唯一方法是:

  1. 将自定义LayoutInflater.Factory设置为onCreateView中的Fragment LayoutInflater

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      super.onCreateView(inflater, container, savedInstanceState);
    
      LayoutInflater layoutInflater = inflater.cloneInContext(inflater.getContext());
      layoutInflater.setFactory(new StarWarsLayoutFactory());
    
      View fragmentView = layoutInflater.inflate(R.layout.jedi, container, false);
      ((TextView) fragmentView.findViewById(android.R.id.jedi)).setText("Yep");
    
      return fragmentView;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 尝试在自定义LayoutInflater.Factory上获取自定义属性:

    public class StarWarsLayoutFactory …
    Run Code Online (Sandbox Code Playgroud)

android android-xml android-fragments android-view android-custom-attributes

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

在我的例子中使用库项目中的自定义视图

我创建了一个 Android主库项目,在其中创建了一个自定义视图类:

package com.my.android.library.layout;

public class CustomView extends View {
...
Run Code Online (Sandbox Code Playgroud)

我还为我定义了styleableCustomView

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="CustomView">
        <attr name="title_text" format="string" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

由于我不想将我的源代码分享给使用我的库项目的其他人,所以我创建了一个分布式库项目,其中我将上述主库项目的 jar 添加到分布式库项目的libs/文件夹中,并从主库项目分布式库项目

接下来,我制作了一个使用分布式库项目android 应用项目。在 app 项目的主布局文件中,我定义了以下内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <com.my.android.library.layout.CustomView
        custom:title_text = "THIS IS TITLE" 
        />
<RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

当我运行我的应用程序时,出现以下异常:

E/AndroidRuntime(30993): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.android.app/com.my.android.app.MainActivity}: android.view.InflateException: Binary XML file line #7: …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-layout android-library android-custom-attributes

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

为子项定义自定义ViewGroup属性

我正在寻找一种方法来定义他们的孩子使用的视图的自定义属性.例如layout_centerInParent,儿童RelativeLayoutslayout_span儿童的儿童TableLayouts.

android android-custom-view android-layout android-custom-attributes

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