我有以下代码来设置android TabLayout文本和图像:
//First tab - Home Facebook Updates
tabLayout.addTab(tabLayout.newTab()
.setText("Tab 1")
.setIcon(R.drawable.fb_updates));
//Second tab - Instagram Latest Checkins
tabLayout.addTab(tabLayout.newTab()
.setText(Tab 2"));
.setIcon(R.drawable.ic_location_on_white_24dp));
//Third tab - Events
tabLayout.addTab(tabLayout.newTab()
.setText("אירועים")
.setIcon(R.drawable.ic_event_note_white_24dp));代码工作良好,但出于某些原因,选项卡图标出现在选项卡文本的上方,而不是文本附近。例如:
选项卡1图像
表1文本
我需要它这样吸引我:
选项卡1图像标签1文本
发布于 2015-11-10 16:16:35
默认的选项卡布局将把图标放在文本的上方。如果你想把图标放在一边,你必须提供一个定制的布局。
注意:不要给ImageView id android.R.id.icon,因为选项卡布局会自动在它下面放一个边距,不管它在哪里!
做这样的事:
View tabView = LayoutInflater.from(context).inflate(R.layout.custom_tab, null, false);
// use findViewById etc to set the text and images
tabLayout.addTab(
tabLayout.newTab()
.setCustomView(tabView )
);R.layout.custom_tab可以是任何你喜欢的东西,但是很可能是这样的:
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_width="@dimen/indicator_size_tab"
android:layout_height="@dimen/indicator_size_tab"
android:id="@+id/tab_icon"
android:layout_marginRight="@dimen/margin_small"
android:layout_marginEnd="@dimen/margin_small"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@android:id/text1"
android:textAppearance="@style/TextAppearance.Design.Tab"
android:textColor="@color/tab_text"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"/>
</LinearLayout>https://stackoverflow.com/questions/33634323
复制相似问题