自定义视图样式,android的属性被忽略

莉娜·布鲁(Lena Bru)

我创建了一个自定义复合视图-该视图已加载,没有崩溃,到目前为止效果很好。

现在,我计划在我的应用程序中多次使用此视图,因此该视图需要一种样式。

我在attr.xml文件中声明了可样式化

  <declare-styleable name="MyCustomView">

        <attr name="ff_label" format="string" />
        <attr name="ff_fieldText" format="string" />
    </declare-styleable>

    <declare-styleable name="MyCustomViewStyle">
        <attr name="customViewStyle" format="reference" />
    </declare-styleable>

然后我去我的主题文件,并写在里面

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    //bunch of other stuff
    <item name="customViewStyle">@style/customViewStyle</item>
</style>

然后在我的Android清单中,我声明了

<application
        android:name="com.my.app.App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

然后在我的styles.xml文件中

   <style name="customViewStyleStyle" parent="@android:style/Widget.EditText">
        <item name="android:paddingBottom">@dimen/space_between_adjacent_widgets_vertical</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="ff_label">@string/default_label_text</item>
        <item name="ff_fieldText">@string/default_label_text</item>
    </style>

我的问题:可以识别我的OWN属性。
为什么将这些属性标记为"android:..."忽略?

MyCustomView.java

public MyCustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    initAttributes(context, attrs, R.attr.customViewStyle);
}

public MyCustomView(Context context) {
    super(context);
    initAttributes(context, null, R.attr.customViewStyle);
}

private void initAttributes(Context context, AttributeSet attrs, int defStyle) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.custom_view, this, true);
    label = (TextView) findViewById(R.id.label);
    formField = (EditText) findViewById(R.id.formField);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle,0);

    if (a.hasValue(R.styleable.MyCustomView_ff_label)) {
        labelText = a.getString(R.styleable.MyCustomView_ff_label);
        label.setText(labelText);
    }


    if (a.hasValue(R.styleable.MyCustomView_ff_fieldText)) {
        fieldText = a.getString(R.styleable.MyCustomView_ff_fieldText);
        field.setHint(fieldText);
    }


    a.recycle();
}

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.my.app"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.my.app.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          />

   <com.my.app.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          />

    <com.my.app.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          />

    <com.my.app.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          />

    <com.my.app.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          />

    <com.my.app.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          />

    <com.my.app.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          />

    <com.my.app.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
          />

    <include layout="@layout/layout_set_default" />

</TableLayout>

1个视图的默认高度约为30 dp->,我无法使用列表视图。每个视图之间应该有10dp填充

gr

您必须在MyCustomView此处更改如下代码

    ...
    public MyCustomView(Context context, AttributeSet attrs) {
            //Called by Android if <com.my.app.MyCustomView/> is in layout xml file without style attribute.
            //So we need to call MyCustomView(Context context, AttributeSet attrs, int defStyle) 
            // with R.attr.customViewStyle. Thus R.attr.customViewStyle is default style for MyCustomView.
            this(context, attrs, R.attr.customViewStyle);
    }

    public MyCustomView(Context context) {
            this(context, null);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
            //Called by Android if <com.my.app.MyCustomView/> is in layout xml with style attribute
            // For example:
            //         <com.my.app.MyCustomView
            //                android:layout_width="match_parent"
            //                android:layout_height="wrap_content"
            //                style="@style/customViewStyleStyle"
            //                />
            //
            super(context, attrs, defStyle);
            initAttributes(context, attrs, defStyle);
    }
    ...

然后,您可以使用样式属性layout.xml,并customViewStyle会也为默认样式MyCustomViewtextViewStyle默认样式相同TextView

    <com.my.app.MyCustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/customViewStyleStyle"
      />

以前,您具有构造函数:

public MyCustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    initAttributes(context, attrs, R.attr.customViewStyle);
}

如您所见,您将传递R.attr.customViewStyleinitAttributes()方法,但不要将其传递给父构造函数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

自定义视图样式,android的属性被忽略

来自分类Dev

在操作栏中实现自定义视图样式

来自分类Dev

通过编程设置的ToggleButton扩展的自定义视图样式

来自分类Dev

使用TileMill自定义OSM地图样式

来自分类Dev

自定义android视图的“自定义类型”属性

来自分类Dev

为什么忽略此自定义视图属性?

来自分类Dev

将自定义地图样式添加到Mapbox.js

来自分类Dev

自定义谷歌地图样式需要一秒钟才能加载

来自分类Dev

Android:如何从自定义视图的超类获取属性

来自分类Dev

Android上的自定义视图和窗口属性

来自分类Dev

Android:自定义视图属性的选择器

来自分类Dev

使用内置属性的Android自定义图像视图

来自分类Dev

动态添加自定义视图并设置其属性android

来自分类Dev

Android自定义视图获取多个属性

来自分类Dev

在 Android 中使用枚举自定义视图属性

来自分类Dev

在Android Studio中找不到我的自定义视图的自定义属性

来自分类Dev

具有自定义属性的Android自定义视图

来自分类Dev

种类!似乎忽略了自定义属性

来自分类Dev

文本溢出属性自定义样式

来自分类Dev

选择自定义属性以设置样式

来自分类Dev

通过自定义视图属性传递样式时,为什么文本不是粗体?

来自分类Dev

自定义文本视图属性出错

来自分类Dev

自定义文本视图属性出错

来自分类Dev

定义要在主题内使用的特定视图样式

来自分类Dev

Android ActionBar自定义布局样式

来自分类Dev

Android上的自定义按钮样式

来自分类Dev

Android ActionBar自定义布局样式

来自分类Dev

Android上的自定义按钮样式

来自分类Dev

如何在Android中使自定义视图继承其父项的样式

Related 相关文章

热门标签

归档