2017-07-21 13 views
6

मैंने android data binding पढ़ा है और इसे अपने एप्लिकेशन, में उपयोग करना चाहता हूं लेकिन मैं एक्सएमएल लेआउट चरण में विफल रहा।टैबहोस्ट लेआउट और डेटा बाइंडिंग

मैं activity_main.xml इस तरह है:

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
<data> 
</data> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_weight="1"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:id="@+id/tab1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <include layout="@layout/tab1"/> 

     </LinearLayout> 

    </FrameLayout> 
</LinearLayout> 
</TabHost> 
</layout> 

और tab1.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
<EditText 
... 

मैं पिछले EditText के लिए बाध्य डेटा लागू करना चाहते हैं, लेकिन मैं

<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <data> 
    </data> 
    <TabHost> 
    ... 

सम्मिलित करता है, तो यह

का कारण बनता है
activity_main.xml:9: AAPT: Error parsing XML: duplicate attribute 

सवाल यह है कि, मैं डेटा बाध्यकारी और TabHost को EditText को शामिल लेआउट में बाध्य करने के लिए कैसे जोड़ूं?

Here is repo with code from question

+0

आपने अपने किसी भी टैग को क्यों बंद नहीं किया है? – Codebender

+1

हमें पूर्ण xml – Salman500

+0

@ सलमान 500 दिखाएं, आप सवाल से xml पा सकते हैं https://github.com/davemilter/TabHostDataBinding/tree/master/app/src/main/res/layout – user1244932

उत्तर

2

यहाँ अपने संकेत XML: duplicate attribute है। यह आपको त्रुटि संदेश, 9 में एक पंक्ति संख्या भी बताता है, जो लगभग टैबहोस्ट तत्व के भीतर है।

अब, कौन सी एक्सएमएल विशेषता डुप्लीकेट है? नाम स्थान (xmlns:android)

एक है कि लेआउट टैग में एक्सएमएल के सबसे ऊपर तत्व

+1

यह लाइन विशेष रूप से, xmlns: android = "http://schemas.android.com/apk/res/android" मुझे नहीं पता कि इस प्रश्न को दो अपवॉट कैसे मिले! – Debanjan

1

अंक xmlns:android

साथ है पर नहीं है बस इस xmlns:android="http://schemas.android.com/apk/res/android" से और उसके किया हटाने निकालें।

DataBinding के बारे में, मैं ऐसा नहीं सोचता तुम भी है कि टैग

अपने activity_main.xml

<data> 

    <variable 
     name="name" 
     type="String"/> 

</data> 

पास उस में <data> ले शामिल लेआउट के साथ

<include layout="@layout/tab1" 
     app:name="@{name}"/> 

अब सिवाय यह लागू कर दिया है अपने tab1.xml

012 के अंदर उस डेटा को पकड़ें
<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 

    <data> 
     <variable 
      name="name" 
      type="String"/> 
    </data> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/edit1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_column="1" 
      android:layout_row="0" 
      android:ems="1" 
      android:inputType="text" 
      android:text="@{name}" /> 
    </LinearLayout> 
</layout> 

आपने लगभग कर लिया है, अब आप केवल आपकी गतिविधि में बाध्यकारी

ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main); 
binding.setName("Email Address"); 
0

दो त्रुटियों मैं यहाँ देख सकते हैं लागू करने की आवश्यकता है, तो आप xmlns नाम अंतरिक्ष दो बार, और tab1 आईडी दो बार इस्तेमाल किया। एक नामस्थान निकालें, और आईडी बदलें।

<LinearLayout 
       android:id="@+id/tab1" /* you used tab1 here as id*/ 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <include layout="@layout/tab1"/> /* you used tab1 here as id */ 
संबंधित मुद्दे