2012-05-21 15 views
7

कृपया मुझे बताएं, मैं कस्टम बटन की डिफ़ॉल्ट पृष्ठभूमि को शून्य कैसे सेट कर सकता हूं।एक कस्टम दृश्य में एक डिफ़ॉल्ट शैली (विशेषताओं) प्रदान करना

मेरा मतलब है ... मैं जानता हूँ कि मैं एक "शैली" जो एंड्रॉयड सेट को परिभाषित कर सकते हैं: "@null" करने के लिए पृष्ठभूमि, और स्पष्ट रूप से अपने लेआउट में शैली लागू करने के लिए उपयोगकर्ताओं के लिए कहें। उदाहरण के लिए:

<style name="MyButton" parent="@android:style/Widget.Button"> 
    <item name="android:background">@null</item> 
</style> 

और

<com.xxx.widget.MyButton 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="@style/MyButton" 
    android:text="MyButton" /> 

कोड से ऊपर अच्छी तरह से काम कर रहा है। लेकिन मैं इस शैली को अपनी कक्षा "माईबटन" में आंतरिक रूप से कैसे लागू कर सकता हूं और उपयोगकर्ताओं को शैली को स्पष्ट रूप से सेट नहीं करने देता हूं?

उदाहरण के लिए, कैसे बनाने के लिए निम्नलिखित लेआउट के रूप में पहले से काम करता है:

<com.xxx.widget.MyButton 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="MyButton" /> 

मैं नीचे के रूप में निर्माता में ऐसा करने की कोशिश की है, लेकिन इसके काम नहीं कर रहा।

public MyButton(Context context, AttributeSet attrs) { 
    this(context, attrs, com.xxx.R.style.MyButton); 
} 

पीएस। मैं इस "शून्य" पृष्ठभूमि को लागू करना चाहता हूं जब कोई उपयोगकर्ता पृष्ठभूमि स्पष्ट रूप से सेट नहीं करता है।

+0

मुझे लगता है कि आप अपने प्रश्न का शीर्षक बदलना चाहिए। आप हमेशा एक डिफ़ॉल्ट शैली कर सकते हैं, लेकिन वह बाद में बदल सकता है। शायद आप 'अंतिम शैली' या – ikbal

+1

जैसे कुछ कहना चाहते हैं http://stackoverflow.com/questions/4493947/how-to-define-theme-style-item-for-custom-widget इस पर कुछ सुझावों के लिए देखें। –

+0

एक समान आवश्यकता थी: http://stackoverflow.com/questions/31504413/theme-style-for-custom-view/31505624#31505624 – stefan

उत्तर

0

अपने मायबटन() कन्स्ट्रक्टर में, सेटबैकग्राउंड (शून्य) क्यों नहीं कॉल करें?

0

यह करने के लिए यह एक सस्ता तरीका है, लेकिन पृष्ठभूमि को एक पारदर्शी वर्ग में क्यों सेट नहीं किया गया है। इस तरह ->

transparent_square.xml: (यह drawable निर्देशिका में रखो)

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/square" 
    android:> 

    <solid android:color="#00808080"></solid> // The first two zeros set the alpha 
               // to 0 

</shape> 

फिर अपने drawable के लिए बटन की पृष्ठभूमि निर्धारित किया है।

बटन:

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send" 
     android:id="@+id/sendButton" 
     android:layout_weight="1" 
     android:background="@drawable/transparent_square"/> 
संबंधित मुद्दे