2017-07-21 4 views
6

Ich lese über android data binding und möchte es in meiner Anwendung verwenden, , aber ich scheiterte auf XML-Layout-Bühne.TabHost Layout und DataBinding

Ich habe activity_main.xml wie folgt aus:

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

und tab1.xml:

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

ich anwenden möchten Daten zur letzten EditText verbindlich, aber wenn ich einfügen

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

dies verursacht

activity_main.xml:9: AAPT: Error parsing XML: duplicate attribute 

Die Frage ist, wie soll ich combind Datenbindung und TabHostEditText in eingeschlossenem Layout zu binden?

Here is repo with code from question

+0

Warum haben Sie nicht alle Ihre Tags geschlossen? – Codebender

+1

zeigen Sie uns volle xml – Salman500

+0

@ Salman500 hier können Sie Xml aus Frage finden https://github.com/davilter/TabHostDataBinding/tree/master/app/src/main/res/layout – user1244932

Antwort

2

Hier ist dein Tipp XML: duplicate attribute. Es teilt Ihnen sogar eine Zeilennummer in der Fehlermeldung 9 mit, die ungefähr innerhalb des TabHost-Elements liegt.

Nun, welches XML-Attribut wird dupliziert? Der Namespace (xmlns:android)

Entfernen Sie die eine, die mit nicht ganz am oberen Element des XML im Layout-Tag

+1

Diese Zeile speziell, xmlns: android = "http://schemas.android.com/apk/res/android" Ich weiß nicht, wie diese Frage zwei upvotes bekam! – Debanjan

1

Ausgabe xmlns:android

ist gerade diese xmlns:android="http://schemas.android.com/apk/res/android" entfernen und ihre getan.

In Bezug auf DataBinding, ich glaube nicht, so haben Sie selbst implementiert es außer diesem Tag

<data> nehmen in Ihrem activity_main.xml

<data> 

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

</data> 

Pass mit eingeschlossenem Layout

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

Jetzt Fangen Sie diese Daten in Ihrem tab1.xml

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

Sie sind fast fertig, jetzt müssen Sie nur implementieren in Ihrer Tätigkeit

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

Zwei Fehler Bindung kann ich hier sehe, verwendet man xmlns Namensraum zwei Mal, und tab1 id zweimal. Entfernen Sie einen Namespace und ändern Sie die ID.

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