Java android studio中的Tabitem绑定
嗨,大家!:) 我需要在选项卡式活动(或片段)选项卡项中自定义布局 首先->我为tabitem定义了这个布局: tab\u item\u layout.xmlJava android studio中的Tabitem绑定,java,android,android-fragments,build.gradle,android-databinding,Java,Android,Android Fragments,Build.gradle,Android Databinding,嗨,大家!:) 我需要在选项卡式活动(或片段)选项卡项中自定义布局 首先->我为tabitem定义了这个布局: tab\u item\u layout.xml <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"&
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:id="@+id/tab_item_root_linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tab_item_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Wink from"/>
<TextView
android:id="@+id/tab_item_number_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
tools:text="1"/>
</LinearLayout>
</layout>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="variableName"
type="packagename.yourViewModel" />
</data>
<android.support.design.widget.TabLayout
android:id="@+id/surf_tab_layout"
android:layout_width="368dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TabItem
android:id="@+id/tabItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/tab_item_layout" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/tab_item_layout" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/tab_item_layout" />
</android.support.design.widget.TabLayout>
</layout>
这可能会对您有所帮助。我知道,我不应该使用
findviewbyd()
,但我还没有办法解决这个问题。如果我得到任何答案,我会更新我的答案
View tabView = binding.surfTabLayout.getTabAt(0).getCustomView();
TextView textView = (TextView) tabView.findViewById(R.id.tab_item_name_text_view);
textView.setText("Hello")
我就是这样做的:
val tabView = binding.tabLayout.getTabAt(0)?.customView
if (tabView != null){
val tabBinding = DataBindingUtil.bind<ViewTabWithBadgeBinding>(tabReceived)
tabBinding?.count = viewModel.tabBadgeCount
tabBinding?.name = viewModel.tabName
}
val tabView=binding.tabLayout.getTabAt(0)?.customView
if(选项卡视图!=null){
val tabBinding=DataBindingUtil.bind(tabReceived)
tabBinding?.count=viewModel.tabBadgeCount
tabBinding?.name=viewModel.tabName
}
其中“binding”是片段/活动的根绑定。我知道这一点。但是我需要tabitem布局项绑定。示例:binding.tabitem1.nameTextView.setText(“asdad”)。编辑:binding.tabItem1为null,但我使用tablayout.getTabAt(0)这不是null,但这是tablayout.tab,我不知道转换为tabitem或其他。
View tabView = binding.surfTabLayout.getTabAt(0).getCustomView();
TextView textView = (TextView) tabView.findViewById(R.id.tab_item_name_text_view);
textView.setText("Hello")
val tabView = binding.tabLayout.getTabAt(0)?.customView
if (tabView != null){
val tabBinding = DataBindingUtil.bind<ViewTabWithBadgeBinding>(tabReceived)
tabBinding?.count = viewModel.tabBadgeCount
tabBinding?.name = viewModel.tabName
}