标签: android-custom-attributes

在预览布局中使用自定义视图中的自定义属性

我使用Android Studio并创建了自定义视图:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:gravity="center_horizontal|center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp">

    <Button
            android:id="@+id/dateTile"
            android:layout_height="@dimen/date_tile_height"
            android:layout_width="@dimen/date_tile_width"
            android:background="@drawable/bg_january"/>

    <CheckBox
            android:id="@+id/dateTileCheckbox"
            android:button="@drawable/check_green"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="-20dp"
            android:focusable="false"/>

    <TextView
            android:id="@+id/dateTileLabel"
            android:layout_width="wrap_content"
            android:layout_marginTop="-10dp"
            android:layout_height="wrap_content"/>

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

另外我定义了以下自定义属性

<resources>
    <declare-styleable name="DateTileView">
        <attr name="monthLabel" format="string" localization="suggested" />
        <attr name="tileBackground" format="reference" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

我使用自定义属性在构造函数中定义日期磁贴的标签和背景,如下所示.

public class DateTileView extends LinearLayout
{
//....
//....
public DateTileView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DateTileView, 0, 0);

        String monthLabel = a.getString(R.styleable.DateTileView_monthLabel);
        Drawable monthBG = a.getDrawable(R.styleable.DateTileView_tileBackground);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-custom-attributes

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

使用obtainStyledAttributes获取多个样式属性

我试图android从我的代码中获取命名空间的几个样式属性.在这里,我附上相关的摘录.AttributeSet attrs是传递给任何自定义的参数TextView.

private static final int[] ATTRS = new int[] { android.R.attr.textSize,
   android.R.attr.text, android.R.attr.textColor,
   android.R.attr.gravity };

private void processAndroidAttributes(final Context context,
   final AttributeSet attrs) {
   final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);
   try {

       final String text = a.getString(1);
       myTextView.setText(text);
       final float textSize = a.getDimensionPixelSize(0, DEFAULT_TEXT_SIZE);
       myTextView.setTextSize(textSize);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我想阅读4中描述的4个属性int[] ATTRS.如你所见,我把它textSize作为这个数组的第一个元素.原因很简单 - 如果我将它换成数组中的第二位,则其值无法正确读取(而是加载提供的默认值).另一方面,文本正确地加载到ATTRS我放置它的数组中的任何位置.我不敢跟的位置偏好试验gravitytextColor,但这种排列他们不工作.

有人可以解释为什么获得行为的不稳定行为?

android android-custom-view android-custom-attributes

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

如何在自定义TextView中获取名称空间"android"的属性

在自定义中TextView我试图获取text属性的值(例如).

TypedArray values = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView);
String text = values.getString(com.android.internal.R.styleable.TextView_text);
Run Code Online (Sandbox Code Playgroud)

但我收到此错误消息:

包com.android.internal.R不存在

那么如何检索TextView的"默认"属性?

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

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

需要来自xml属性自定义小部件的图像ID

我有一个自定义控件(现在很简单)就像一个按钮.它需要显示未按下和按下的图像.它在活动中出现多次,并且具有不同的图像对,具体取决于它的使用位置.想想工具栏图标 - 与此类似.

这是我的布局的摘录:

<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:MyApp="http://schemas.android.com/apk/res/com.example.mockup"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >

  <TableRow>
    <com.example.mockup.ImageGestureButton
      android:id="@+id/parent_arrow"
      android:src="@drawable/parent_arrow"
      MyApp:srcPressed="@drawable/parent_arrow_pressed"
      ... />
     ...
  </TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)

attrs.xml:

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

并且,在R.java中,人们发现:

public static final class drawable {
    public static final int parent_arrow=0x7f020003;
    public static final int parent_arrow_pressed=0x7f020004;
    ...
}
Run Code Online (Sandbox Code Playgroud)

在窗口小部件实例化期间,我想确定活动xml中声明的id.我怎么做?我试过这个(我使用工作代码更新了我原来的帖子;所以,以下工作.)

public class ImageGestureButton extends ImageView
   implements View.OnTouchListener
{
  private Drawable unpressedImage;
  private Drawable pressedImage;

  public ImageGestureButton (Context context, AttributeSet attrs)
  {
    super(context, attrs);
    setOnTouchListener (this);

    unpressedImage = …
Run Code Online (Sandbox Code Playgroud)

android custom-widgets android-drawable android-custom-attributes

4
推荐指数
1
解决办法
5719
查看次数

风格中的自定义属性

在我当前的项目styles.xml中,我尝试定义:

<resources xmlns:ripple="http://schemas.android.com/apk/res-auto">
<resources xmlns:ripple="http://schemas.android.com/apk/res/com.gorkem.components">
Run Code Online (Sandbox Code Playgroud)

我用这个:

<style name="FlatTextRipple">
    <item name="ripple:rv_centered">true</item>
    <item name="ripple:rv_color">#CCCCCC</item>
    <item name="ripple:rv_type">simpleRipple</item>
    <item name="ripple:rv_zoom">true</item>
    <item name="ripple:rv_zoomDuration">300</item>

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

在我的库项目中我有attrs.xml:

<declare-styleable name="RippleView">
    <attr name="rv_alpha" format="integer" />
    <attr name="rv_framerate" format="integer" />
    <attr name="rv_rippleDuration" format="integer" />
    <attr name="rv_zoomDuration" format="integer" />
    <attr name="rv_color" format="color" />
    <attr name="rv_centered" format="boolean" />
    <attr name="rv_type" format="enum">
        <enum name="simpleRipple" value="0" />
        <enum name="doubleRipple" value="1" />
        <enum name="rectangle" value="2" />
    </attr>
    <attr name="rv_ripplePadding" format="dimension" />
    <attr name="rv_zoom" format="boolean" />
    <attr name="rv_zoomScale" format="float" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

但我不明白Í什么时候使用它就像在布局中运行它但是当我使用style.xml gradle时不能attr它会给我这个错误:

    Error:(6, 21) No …
Run Code Online (Sandbox Code Playgroud)

android custom-attributes android-custom-attributes android-gradle-plugin

4
推荐指数
1
解决办法
1193
查看次数

在Android中,如何使用new Button()设置自定义属性?

我使用以下方法声明一个按钮:

Button btn = new Button(this);
btn.setText(itemSet[i]);
btn.setId(i);
Run Code Online (Sandbox Code Playgroud)

我需要添加一个自定义属性,该属性将在单击时使用。

有没有办法做到这一点?

android android-button android-custom-attributes

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

尝试使用自定义属性时,标记片段找到了意外的名称空间前缀"custom"

我正在关注这个Android开发者页面,以便为应用程序创建自定义属性.一切都很顺利,我也可以编译并查看结果.但是提出问题的部分是IDE.Android Studio抱怨意外的命名空间自定义.有没有办法禁用/隐藏此错误消息?这非常烦人.

但是,即使出现此错误,我也可以编译并运行该应用程序.

在此输入图像描述

我遵循了以下步骤.

1)创建res/values/attrs.xml文件并添加

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

2)尝试在我的mainlayout文件的片段标记中使用

    <LinearLayout xmlns:custom="http://schemas.android.com/apk/res"
                          android:layout_width="fill_parent"
                          android:layout_height="0dip"
                          android:layout_weight="1">
                <fragment android:id="@+id/msg"
                          android:name="org.android.fragment.MessageFragment"
                          custom:max_allowed="85"
                          custom:header="@string/header"
                          android:layout_width="wrap_content"
                          android:layout_height="match_parent"/>
            </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

3.通过代码覆盖片段的onInflate()方法应用自定义属性

   @Override
    public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
        super.onInflate(activity, attrs, savedInstanceState);

        TypedArray typedArray = activity.obtainStyledAttributes(attrs, R.styleable.FragArguments);
        max_allowed = typedArray.getInt(R.styleable.FragArguments_max_allowed, -1);
        header = typedArray.getString(R.styleable.FragArguments_header);
        typedArray.recycle();
    }
Run Code Online (Sandbox Code Playgroud)

android-layout android-studio android-custom-attributes

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

使用自定义XML标记和数据绑定构建不成功的Gradle

当我尝试使用数据绑定和绑定布局中的自定义XML标记运行项目时,我收到下面描述的构建错误:

布局:

<data>
   <variable
        name="data"
        type="com.melontech.sff.viewmodel.DataViewModel" />
</data> 

...

<ImageView 
   android:id="@+id/image"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@mipmap/placeholder_img"
   app:imageUrl="@{data.imageUrl}" />

...
Run Code Online (Sandbox Code Playgroud)

View Model具有以下Binding适配器:

@BindingAdapter("bind:imageUrl")
public static void getImageUrl(ImageView imageView, String url) {
    Timber.d("LOAD IMAGE WITH URL %s", url);
}
Run Code Online (Sandbox Code Playgroud)

创建ViewHolder并进行绑定的代码如下:

@Override
public ModelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ModelListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.model_list_item, parent, false);
    return new ModelViewHolder(binding);
}
Run Code Online (Sandbox Code Playgroud)

构建错误:

Error:Execution failed for task ':app:transformJackWithJackForDebug'.
Run Code Online (Sandbox Code Playgroud)

com.android.jack.ir.JNodeInternalError:java.lang.Exception:java.lang.RuntimeException:failure,请参阅日志以获取详细信息.无法生成视图绑定器java.lang.IllegalArgumentException:element public default java.util.stream.IntStream codePoints()不是包含类型java.lang.String的成员,也不是com.android.jack.eclipse中的任何超类.位于android.databinding.tool.reflection.annotation的android.databinding.tool.reflection.annotation.AnnotationMethod.(AnnotationMethod.java:49)中的jdt.internal.compiler.apt.model.TypesImpl.asMemberOf(TypesImpl.java:129) .AnnotationClass.getDeclaredMethods(AnnotationClass.java:314)位于android.databinding.tool.exol.exol.MethodBaseExpr.resolveListenersAsMethodReference(MethodBaseExpr.java:71)的android.databinding.tool.reflection.ModelClass.getAbstractMethods(ModelClass.java:401)在android.databinding.tool.BindingTarget.resolveListeners(BindingTarget.java)的android.databinding.tool.Binding.resolveListeners(Binding.java:65)的android.databinding.tool.expr.FieldAccessExpr.resolveListeners(FieldAccessExpr.java:131): 164)在android.databinding.tool.LayoutBinder.(LayoutBinder.java:250)位于android.databinding.tool.DataBinder上的android.databinding.tool.DataBinder.(DataBinder.java:52)android.databinding.tool.CompilerChef.ensureDataBinder(CompilerChef.java:88) (CompilerChef.java:187)在android.databinding.annotationprocessor.ProcessExpressions.writeResourceBundle(ProcessExpressions.java:184)在android.databinding.annotationprocessor.ProcessExpressions.onHandleStep(ProcessExpressions.java:86)在android.databinding.annotationprocessor.ProcessDataBinding $ ProcessingStep.runStep(ProcessDataBinding.java:189)位于android的DB.databprocess.Arocessprocess.ProcessDataBinding $ ProcessingStep.access $ 000(ProcessDataBinding.java:174),位于com的android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:79). com.android.jack.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.round(RoundDispatc)中的android.jack.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:139)her.java:121)位于com.android.jack.eclipse.jdt.internal.compiler的com.android.jack.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotationProcessorManager.processAnnotations(BaseAnnotationProcessorManager.java:159). Comiler.processAnnotations(Compiler.java:909)位于com.android.jack.frontend.java.JAstBuilder.compile的com.android.jack.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:434) JAstBuilder.java:269)com.android.jack.frontend.java.JackBatchCompiler.performCompilation(JackBatchCompiler.java:219)at com.android.jack.eclipse.jdt.internal.compiler.batch.Main.compile(Main. java:1712)com.android.jack.frontend.java.JackBatchCompiler.compile(JackBatchCompiler.java:184)at …

data-binding android gradle android-custom-attributes android-databinding

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