Android中的定制表格布局
我正在尝试建立一个自定义的TabLayout作为库的一部分,它可以被其他应用程序使用。此TableLayout中的每个选项卡还需要具有自定义背景、属性Android中的定制表格布局,android,android-tablayout,Android,Android Tablayout,我正在尝试建立一个自定义的TabLayout作为库的一部分,它可以被其他应用程序使用。此TableLayout中的每个选项卡还需要具有自定义背景、属性 public class MyTab extends TabLayout { Context mContext; public MyTab(Context context) { this(context, null); } public MyTab(Cont
public class MyTab extends TabLayout {
Context mContext;
public MyTab(Context context) {
this(context, null);
}
public MyTab(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyTab(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setBackground(context.getResources().getDrawable(R.drawable.tab_background));
setTabMode(MODE_SCROLLABLE);
mContext = context;
setSelectedTabIndicatorColor(context.getResources().getColor(R.color.lightest_grey9));
}
@Override
public void addTab(@NonNull Tab tab) {
super.addTab(tab);
setTabGravity(GRAVITY_FILL);
View tabView = LayoutInflater.from(mContext).inflate(R.layout.tab_item_background,null);
TextView textView = (TextView) tabView.findViewById(R.id.tab_text);
textView.setText(tab.getText());
// textView.setFocusable(true);
tabView.setFocusable(true);
tabView.setFocusableInTouchMode(true);
// textView.setBackground(getContext().getResources().getDrawable(R.drawable.tab_item_view_background));
tabView.setBackground(getContext().getResources().getDrawable(R.drawable.tab_item_view_background));
tab.setCustomView(tabView);
}
}
我的要求是:
tab_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:focusableInTouchMode="true"
android:id="@+id/tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/lightest_grey9" android:background="@drawable/tab_item_view_background"/>
</LinearLayout>
tab_focused.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<padding android:left="5dp" android:right="5dp"/>
<size android:height="38dp" android:width="40dp"/>
<stroke android:width="2dp"
android:color="@color/lightest_blue5"/>
</shape>
tab_item_view_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/tab_focused">
</item>
</selector>
tab\u item\u background.xml
tab_focused.xml
选项卡\u项目\u视图\u background.xml
欢迎使用Stack Overflow!我编辑了您的问题以使其更具可读性。有关格式设置的详细信息,请查看。另外,请尽量不要让您的问题过于宽泛,或一次问太多问题,并尝试提供一个解决方案。有时太多的信息或代码实际上可能会使人们更难回答您的问题。祝您好运!谢谢@马修斯拉塞尔达