TabHost 布局和数据绑定
TabHost layout and DataBinding
我阅读了 android data binding 并想在我的应用程序中使用它,
但我在 xml 布局阶段失败了。
我有 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
绑定到included布局中绑定EditText
?
这是你的提示 XML: duplicate attribute
。它甚至会告诉您错误消息中的行号 9,大致位于 TabHost 元素内。
现在,哪个 XML 属性重复了?命名空间 (xmlns:android
)
删除不在布局标签 XML 最顶部的元素
问题出在 xmlns:android
只需删除此 xmlns:android="http://schemas.android.com/apk/res/android"
即可完成。
关于 DataBinding
,我认为你甚至没有实现它,除了那个标签
在你的activity_main.xml
中加入<data>
<data>
<variable
name="name"
type="String"/>
</data>
通过包含的布局传递它
<include layout="@layout/tab1"
app:name="@{name}"/>
现在在您的 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>
您几乎完成了,现在您只需要在 activity
中实现绑定
ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
binding.setName("Email Address");
我在这里看到了两个错误,您使用了两次 xmlns
name space,两次使用了 tab1
id。删除一个名字space,并更改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 */
我阅读了 android data binding 并想在我的应用程序中使用它, 但我在 xml 布局阶段失败了。
我有 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
绑定到included布局中绑定EditText
?
这是你的提示 XML: duplicate attribute
。它甚至会告诉您错误消息中的行号 9,大致位于 TabHost 元素内。
现在,哪个 XML 属性重复了?命名空间 (xmlns:android
)
删除不在布局标签 XML 最顶部的元素
问题出在 xmlns:android
只需删除此 xmlns:android="http://schemas.android.com/apk/res/android"
即可完成。
关于 DataBinding
,我认为你甚至没有实现它,除了那个标签
在你的activity_main.xml
<data>
<data>
<variable
name="name"
type="String"/>
</data>
通过包含的布局传递它
<include layout="@layout/tab1"
app:name="@{name}"/>
现在在您的 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>
您几乎完成了,现在您只需要在 activity
中实现绑定ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
binding.setName("Email Address");
我在这里看到了两个错误,您使用了两次 xmlns
name space,两次使用了 tab1
id。删除一个名字space,并更改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 */