2017-07-21 10 views
6

私は約android data bindingを読んで、私のアプリケーションでは を使っていますが、xmlレイアウトステージで失敗しました。TabHostレイアウトとデータバインディング

<?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を持って

これにより、

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

質問は、データバインディングをどのように組み合わせてTabHostEditTextにバインドするのがいいですか?

Here is repo with code from question

+0

なぜあなたはタグを一切閉じていませんか? – Codebender

+1

私たちにフルxmlを表示 – Salman500

+0

@ Salman500ここから質問からxmlを見つけることができますhttps://github.com/davemilter/TabHostDataBinding/tree/master/app/src/main/res/layout – user1244932

答えて

2

はここにあなたのヒントXML: duplicate attributeです。エラーメッセージ9の行番号も表示されます。これは、TabHost要素内にあります。

今、どのXML属性が複製されていますか?名前空間(xmlns:android

レイアウトタグにXMLの最上部の要素ではないものを削除し

+1

この行は、xmlns:android = "http://schemas.android.com/apk/res/android" と具体的にはどのようにこの質問に2つのアップボックスがあるのか​​わかりません! – Debanjan

1

号がジャストからこのxmlns:android="http://schemas.android.com/apk/res/android"を削除し、その完了xmlns:android

です。

DataBindingに関しては、私はあなたがさえ

が含まれるレイアウトに

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

を渡し、あなたのactivity_main.xml

<data> 

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

</data> 

<data>を取り、そのタグを除いて、それを実装しているそうは思いませんそのデータをあなたの内部でキャッチする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> 

が、今はちょうど私がここで見ることができ、あなたの活動に

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

2つのエラーを結合実装する必要がある、あなたはxmlnsネームスペースを2回、そしてtab1 ID 2回使用しました。 1つの名前空間を削除し、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 */ 
関連する問題