简体   繁体   English

将自定义属性的值从父视图级联到子视图?

[英]Cascade the value of a custom attribute from a parent view to a child view?

How do I "cascade" the value of a custom attribute from a parent view to its child view? 如何将自定义属性的值从父视图“级联”到其子视图?

This is easiest to explain using an example: 使用示例最容易解释:

<com.example.CustomLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:percent="35" >

    <com.example.CustomView
        android:id="@+id/customView1"
        app:percent="how-to-get-app:percent-value-here???"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</com.example.CustomLayout>

Here, CustomLayout extends LinearLayout . 在这里, CustomLayout扩展了LinearLayout I have defined a custom attribute "percent" in attrs.xml using <declare-styleable> element. 我使用<declare-styleable>元素在attrs.xml定义了一个自定义属性“percent”。 As you can see, I set percent to 35 in the XML for CustomLayout . 如您所见,我在CustomLayout的XML中将百分比设置为35。

I now want to pass in the same value to CustomView (which extends View ) and which I will include in CustomLayout . 我现在想要将相同的值传递给CustomView (它扩展View ),我将包含在CustomLayout I am unable to find a way to do this in XML (it is easy to do this in code though). 我无法找到在XML中执行此操作的方法(尽管在代码中很容易这样做)。

I tried the following: 我尝试了以下方法:

app:percent="@attr/percent"

app:percent="?attr/percent"

Both of these (expectedly) fail with NumberFormatException at TypedArray#getInt() . TypedArray#getInt() ,这两个(预期)都会因NumberFormatException失败。

So, any ideas on how to get this to work? 那么,关于如何使其发挥作用的任何想法?

Although the idea comes a bit late and the method is not straight forward, I think it's still worth sharing. 虽然这个想法有点迟了,而且方法并不简单,但我认为它仍然值得分享。 We can put custom attributes into a theme, so the attributes can be passed to all child views from the parent view where the theme is used (ie, the attributes exist within the View Group). 我们可以将自定义属性放入主题中,因此可以将属性从使用主题的父视图传递给所有子视图(即视图组中存在属性)。

Example as follows: 示例如下:

<integer name="percentage">35</integer>

<style name="CustomTheme" parent="suitable theme for your case">
    <item name="percent">@integer/percentage</item>
</style>

<com.example.CustomLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:theme="@style/CustomTheme" >

    <com.example.CustomView
        android:id="@+id/customView1"
        app:percent="?attr/percent" <!--Note: that's how it refers to the value,
        however, re-assign the attribute value here is meaningless as attribute percent
        should exist in all child views now. You can retrieve its value via
        Theme.obtainStyledAttributes(R.style.CustomTheme, new int[] {R.attr.percent})
        in every child view-->
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</com.example.CustomLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Android的父视图中的自定义子视图中获取价值 - how to get value from custom child view in parent view in android Android:如何从子自定义视图调用父Activity方法? - Android: How to call a parent Activity method from a child custom view? 自定义视图从 Android 中的自定义主题属性获取自定义属性值 - Custom view get custom attribute value from custom theme attribute in Android 将触摸处理从父视图更改为子视图 - Changing touch handling from parent to child view android将事件从子视图传递到父视图 - android passing events from child view to parent 有没有一种方法可以将alpha属性设置为子视图和父视图? - Is there a way to set alpha attribute to child views as well as to parent view? 从子片段调用父视图 - 视图寻呼机 - Calling parent view from child fragment - view pager 自定义视图的子视图getMeasureWidth在不同的时间返回不同的值 - child view of custom view getMeasureWidth return different value at different time 触摸自定义子视图时如何实现父活动代码? - How to implement the parent activity code when custom child view is touched? Android:在自定义视图中获取父级布局宽度以设置子级宽度 - Android: get parent layout width in custom view to set child width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM