2013-02-27 34 views
11

मैं मूल दृश्य से अपने बच्चे के दृश्य में कस्टम विशेषता के मूल्य को "कैस्केड" कैसे करूं?एक मूल दृश्य से एक बच्चे के दृश्य में एक कस्टम विशेषता का मूल्य कैस्केड?

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

यहाँ, CustomLayout फैली LinearLayout:

यह एक उदाहरण का उपयोग करते हुए समझाने के लिए सबसे आसान है। मैंने <declare-styleable> तत्व का उपयोग कर attrs.xml में एक कस्टम विशेषता "प्रतिशत" परिभाषित की है। जैसा कि आप देख सकते हैं, मैं XML में CustomLayout के लिए प्रतिशत में 35 सेट करता हूं।

अब मैं उसी मान में CustomView (जो View तक फैलाता है) में पास करना चाहता है और जिसमें मैं CustomLayout में शामिल होगा। मैं XML में ऐसा करने का कोई तरीका नहीं ढूंढ पा रहा हूं (हालांकि कोड में ऐसा करना आसान है)।

app:percent="@attr/percent"

app:percent="?attr/percent"

के इन (प्रत्याशित) NumberFormatException के साथ विफल TypedArray#getInt() पर

दोनों:

मैं निम्नलिखित की कोशिश की।

तो, इस काम को कैसे प्राप्त किया जाए इस पर कोई विचार है?

+0

आप एक समाधान मिला? इसके अलावा किसी भी मौके से @ कॉमन्सवेयर क्या आपने कभी ऐसा कुछ करने की कोशिश की है? मैं इस काम को कैसे प्राप्त करें इस पर विचारों से बाहर हूं ... –

+0

@ एंटोनियोई। नहीं, कभी रास्ता नहीं मिला। इसके बजाय जावा कोड में इसे समाप्त कर दिया। – curioustechizen

+0

निराश है कि इसके लिए कोई जवाब नहीं है। – Everett

उत्तर

1

हालांकि विचार थोड़ा देर हो गया है और विधि सीधे आगे नहीं है, मुझे लगता है कि यह अभी भी साझा करने लायक है। हम कस्टम एट्रिब्यूट्स को थीम में डाल सकते हैं, इसलिए गुणों को मूल दृश्य से सभी बच्चे के विचारों में पारित किया जा सकता है जहां विषय का उपयोग किया जाता है (यानी, व्यू समूह के भीतर गुण मौजूद हैं)।

उदाहरण इस प्रकार है:

<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> 
संबंधित मुद्दे