Android 片段中的自定义属性 [英] Custom attributes in Android fragments

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

问题描述

我想使用 XML(不使用捆绑附加参数)在 Android 片段中定义自定义属性,例如自定义控件中的 declare-styleable.但是没有带 AttrSet 参数的构造函数,所以有可能吗?我可以覆盖 public void onInflate(android.app.Activity activity, android.util.AttributeSet attrs, android.os.Bundle savedInstanceState) 以获得属性支持吗?

I'd like to define custom attributes in Android fragment using XML (without using bundle additional parameters) like declare-styleable in custom controls. But there are no constructors with AttrSet parameters, so is it possible? Can i just override public void onInflate(android.app.Activity activity, android.util.AttributeSet attrs, android.os.Bundle savedInstanceState) in order to get attributes support?

推荐答案

Support4Demos 的链接已更改或可以更改,因此发布完整的解决方案.就到这里了.

The Link for Support4Demos is changed or can be changed so posting the complete solution. Here it goes.

  1. 在 res/values 文件夹中创建 attrs.xml 文件.或者,如果文件已经存在,则添加以下内容.

  1. Create attrs.xml file in res/values folder. Or add the below content if file already exists.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyFragment">
    <attr name="my_string" format="string"/>
    <attr name="my_integer" format="integer"/>
</declare-styleable> 

覆盖片段的 onInflate 委托并读取其中的属性

Override the onInflate delegate of fragment and read attributes in it

/**
 * Parse attributes during inflation from a view hierarchy into the
 * arguments we handle.
 */
@Override
public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
    super.onInflate(activity, attrs, savedInstanceState);
    Log.v(TAG,"onInflate called");

    TypedArray a = activity.obtainStyledAttributes(attrs,R.styleable.MyFragment);

    CharSequence myString = a.getText(R.styleable.MyFragment_my_string);
    if(myString != null) {
        Log.v(TAG, "My String Received : " + myString.toString());
    }

    int myInteger = a.getInt(R.styleable.AdFragment_my_integer, -1);
    if(myInteger != -1) {
        Log.v(TAG,"My Integer Received :" + myInteger);
    }

    a.recycle();
}

  • 在您的布局文件中传递这些属性,如下所示.举个例子

  • Pass these attributes in your layout file as following. Just an example

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is android activity" />
    
        <fragment
            android:id="@+id/ad_fragment"
            android:name="com.yourapp.packagename.MyFragment"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            app:my_string="Hello This is HardCoded String. Don't use me"
            app:my_integer="30" />
    
    </RelativeLayout>
    

  • 仅此而已.它是一个有效的解决方案.

    Thats all. Its a working solution.

    执行此操作时,如果您在 xml 中看到任何命名空间错误.一次又一次地尝试清理项目.这很可悲,但 eclipse 和 adt 有时会行为不端.

    While doing this if you see any namespace error in xml. try project cleaning again and again. This is pathetic but eclipse and adt misbehaves sometimes.

    希望它可以帮助其他人:)

    Hope it helps others :)

    干杯

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

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