<bdo id='nMpGD'></bdo><ul id='nMpGD'></ul>
    1. <i id='nMpGD'><tr id='nMpGD'><dt id='nMpGD'><q id='nMpGD'><span id='nMpGD'><b id='nMpGD'><form id='nMpGD'><ins id='nMpGD'></ins><ul id='nMpGD'></ul><sub id='nMpGD'></sub></form><legend id='nMpGD'></legend><bdo id='nMpGD'><pre id='nMpGD'><center id='nMpGD'></center></pre></bdo></b><th id='nMpGD'></th></span></q></dt></tr></i><div id='nMpGD'><tfoot id='nMpGD'></tfoot><dl id='nMpGD'><fieldset id='nMpGD'></fieldset></dl></div>
      <legend id='nMpGD'><style id='nMpGD'><dir id='nMpGD'><q id='nMpGD'></q></dir></style></legend>

    2. <tfoot id='nMpGD'></tfoot>

      <small id='nMpGD'></small><noframes id='nMpGD'>

        如何在 java 和 xml 中传递自定义组件参数

        How to pass custom component parameters in java and xml(如何在 java 和 xml 中传递自定义组件参数)
          <tbody id='27GbR'></tbody>
          <legend id='27GbR'><style id='27GbR'><dir id='27GbR'><q id='27GbR'></q></dir></style></legend>
            <bdo id='27GbR'></bdo><ul id='27GbR'></ul>
          • <i id='27GbR'><tr id='27GbR'><dt id='27GbR'><q id='27GbR'><span id='27GbR'><b id='27GbR'><form id='27GbR'><ins id='27GbR'></ins><ul id='27GbR'></ul><sub id='27GbR'></sub></form><legend id='27GbR'></legend><bdo id='27GbR'><pre id='27GbR'><center id='27GbR'></center></pre></bdo></b><th id='27GbR'></th></span></q></dt></tr></i><div id='27GbR'><tfoot id='27GbR'></tfoot><dl id='27GbR'><fieldset id='27GbR'></fieldset></dl></div>
            • <tfoot id='27GbR'></tfoot>

              <small id='27GbR'></small><noframes id='27GbR'>

                  本文介绍了如何在 java 和 xml 中传递自定义组件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 android 中创建自定义组件时,经常会被问到如何创建 attrs 属性并将其传递给构造函数.

                  When creating a custom component in android it is often asked how to create and pass through the attrs property to the constructor.

                  通常建议在java中创建组件时,只需使用默认构造函数,即

                  It is often suggested that when creating a component in java that you simply use the default constructor, i.e.

                  new MyComponent(context);
                  

                  而不是尝试创建一个 attrs 对象以传递给基于 xml 的自定义组件中常见的重载构造函数.我试图创建一个 attrs 对象,但它似乎既不容易或根本不可能(没有非常复杂的过程),而且从所有角度来看,这并不是真正需要的.

                  rather than attempting to create an attrs object to pass through to the overloaded constructor often seen in xml based custom components. I've tried to create an attrs object and it doesn't seem either easy or at all possible (without an exceedingly complicated process), and by all accounts isn't really required.

                  然后我的问题是:在 java 中构造一个自定义组件的最有效方法是什么,该组件传递或设置在使用 xml 对组件进行膨胀时本来由 attrs 对象设置的属性?

                  My question is then: What is the most efficient way of construction a custom component in java that passes or sets properties that would have otherwise been set by the attrs object when inflating a component using xml?

                  推荐答案

                  (完全披露:这个问题是创建自定义视图)

                  (Full disclosure: This question is an offshoot of Creating custom view)

                  除了从 View 继承的三个标准构造函数之外,您还可以创建添加所需属性的构造函数...

                  You can create constructors beyond the three standard ones inherited from View that add the attributes you want...

                  MyComponent(Context context, String foo)
                  {
                    super(context);
                    // Do something with foo
                  }
                  

                  ...但我不推荐它.最好遵循与其他组件相同的约定.这将使您的组件尽可能灵活,并防止使用您的组件的开发人员因为您的组件与其他所有内容不一致而撕毁他们的头发:

                  ...but I don't recommend it. It's better to follow the same convention as other components. This will make your component as flexible as possible and will prevent developers using your component from tearing their hair out because yours is inconsistent with everything else:

                  1.为每个属性提供 getter 和 setter:

                  public void setFoo(String new_foo) { ... }
                  public String getFoo() { ... }
                  

                  <强>2.在 res/values/attrs.xml 中定义属性,以便它们可以在 XML 中使用.

                  2. Define the attributes in res/values/attrs.xml so they can be used in XML.

                  <?xml version="1.0" encoding="utf-8"?>
                  <resources>
                    <declare-styleable name="MyComponent">
                      <attr name="foo" format="string" />
                    </declare-styleable>
                  </resources>
                  

                  3.提供 View 中的三个标准构造函数.

                  3. Provide the three standard constructors from View.

                  如果您需要从一个采用 AttributeSet 的构造函数中的属性中挑选任何内容,您可以这样做...

                  If you need to pick anything out of the attributes in one of the constructors that takes an AttributeSet, you can do...

                  TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MyComponent);
                  CharSequence foo_cs = arr.getString(R.styleable.MyComponent_foo);
                  if (foo_cs != null) {
                    // Do something with foo_cs.toString()
                  }
                  arr.recycle();  // Do this when done.
                  

                  完成所有这些后,您就可以通过编程方式实例化 MyCompnent...

                  With all that done, you can instantiate MyCompnent programmatically...

                  MyComponent c = new MyComponent(context);
                  c.setFoo("Bar");
                  

                  ...或通过 XML:

                  ...or via XML:

                  <!-- res/layout/MyActivity.xml -->
                  <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:blrfl="http://schemas.android.com/apk/res-auto"
                    ...etc...
                  >
                    <com.blrfl.MyComponent
                     android:id="@+id/customid"
                     android:layout_weight="1"
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent"
                     android:layout_gravity="center"
                     blrfl:foo="bar"
                     blrfl:quux="bletch"
                    />
                  </LinearLayout>
                  

                  其他资源 - https://developer.android.com/training/custom-views/create-view

                  这篇关于如何在 java 和 xml 中传递自定义组件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
                  SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
                  Android file copy(安卓文件拷贝)
                  Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)
                  Android copy image from gallery folder onto SD Card alternative folder(Android将图像从图库文件夹复制到SD卡替代文件夹)
                  Is there a tool to find unused resources in an Android project?(是否有工具可以在 Android 项目中查找未使用的资源?)

                  • <tfoot id='Lwzir'></tfoot>

                  • <i id='Lwzir'><tr id='Lwzir'><dt id='Lwzir'><q id='Lwzir'><span id='Lwzir'><b id='Lwzir'><form id='Lwzir'><ins id='Lwzir'></ins><ul id='Lwzir'></ul><sub id='Lwzir'></sub></form><legend id='Lwzir'></legend><bdo id='Lwzir'><pre id='Lwzir'><center id='Lwzir'></center></pre></bdo></b><th id='Lwzir'></th></span></q></dt></tr></i><div id='Lwzir'><tfoot id='Lwzir'></tfoot><dl id='Lwzir'><fieldset id='Lwzir'></fieldset></dl></div>

                    <small id='Lwzir'></small><noframes id='Lwzir'>

                      <legend id='Lwzir'><style id='Lwzir'><dir id='Lwzir'><q id='Lwzir'></q></dir></style></legend>

                      • <bdo id='Lwzir'></bdo><ul id='Lwzir'></ul>
                            <tbody id='Lwzir'></tbody>