自定义视图样式,Android的属性被忽略 [英] Custom view style, android's attributes are ignored

查看:341
本文介绍了自定义视图样式,Android的属性被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个定制的复合视图 - 视图负载,没有崩溃,到目前为止好

I created a custom compound view - the view loads, no crashes, so far so good.

现在,我计划在我的应用程序来使用这个观点很多很多次,所以认为需要的风格。

Now, i plan to use this view MANY MANY times in my app, so the view needs a style.

我宣布为它设置样式在我attr.xml文件

I declared a styleable for it in my attr.xml file

  <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>

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

Then i went to my theme file and wrote this inside

<!-- 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>

然后在我的Andr​​oid清单我宣布

then in my android manifest i declared

<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文件我写了

then in my styles.xml file i wrote

   <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>

我的问题:自己的特性被认可就好了结果。
属性为何被标记机器人:...?忽略

MyCustomView.java

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

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填充

The default height of 1 view is about 30 dp - > and i can't use a list view for it. Its supposed to have 10dp padding between each view

推荐答案

您必须修改 MyCustomView 的code喜欢这里:

You have to change the code of MyCustomView like here:

    ...
    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 会还的默认样式<$使用样式属性C $ C> MyCustomView 。同为 textViewStyle 的TextView

Then you can use style attribute in layout.xml and customViewStyle would be also the default style for MyCustomView. The same as textViewStyle is default style for TextView

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

previously你有构造函数:

Previously you had the constructor:

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

正如你可以看到你通过 R.attr.customViewStyle initAttributes()方法,但不及格它父类的构造。

As you can see you pass R.attr.customViewStyle to initAttributes() method but don't pass it to parent constructor.

这篇关于自定义视图样式,Android的属性被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆