【问题标题】:Android & custom views & custom xml attributesAndroid & 自定义视图 & 自定义 xml 属性
【发布时间】:2013-01-16 18:42:14
【问题描述】:

假设我有一个有效的自定义视图,名为 com.package.MyCustomView(MyCustomView 扩展了 LinearLayout 或任何视图)

我可以在这样的活动布局中使用它

<com.package.MyCustomView
        android:id="@+id/myView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

但我想要这样的东西:

<com.package.MyCustomView
        android:id="@+id/myView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        MyCustomView_attribute1="100"
        MyCustomView_attribute2="fantastic"/>

我想如果像 android:layout_width 这样的有效 android xml 属性,关联的value (match_parent, wrap_content) 将设置在父 View 对象的特定属性上(通过使用反射或其他方式。 . ).我也知道命名空间的存在(那行:xmlns:android="http://schemas.android.com/apk/res/android")..

在任何情况下,我都想要相同类型的东西 - 为我的视图自定义自定义 xml 属性...

这可能吗?如果是 - 如何?请提供一些细节

【问题讨论】:

    标签: android xml view custom-controls


    【解决方案1】:

    声明一个可样式化的资源和一些自定义属性。 Official android tutorial.

    【讨论】:

      【解决方案2】:

      是的,你可以这样做......它会工作

        <com.example.application.Drawer
               xmlns:my="http://schemas.android.com/apk/res/com.example.application"
               android:id="@+id/drawer"
               android:layout_width="301dp"
               android:layout_height="wrap_content"
               my:content="@+id/content"
               my:handle="@+id/handle" >
      

      但有一件事是你应该在src中有上面的包

      【讨论】:

        猜你喜欢
        • 2013-09-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        相关资源
        最近更新 更多