XML 布局
<?xml version="1.0" encoding="utf-8"?><com.c03.jy.wanandroid.playandroid.view.TabBottomLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:id="@+id/main_bottom_tab_layout"
android:background="#FFFFFF"
android:layout_height=“match_parent”>
<RadioButton
android:id="@+id/tablayout_rb_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:button="@null"
android:drawableTop="@drawable/ic_home_black_24dp"
android:gravity="center_horizontal"
android:text="@string/tabBottom_shoye"
android:textColor="@drawable/main_bottom_tab_text_color_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tablayout_rb_knowledge"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<RadioButton
android:id="@+id/tablayout_rb_knowledge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableTop="@drawable/ic_apps_black_24dp"
android:textColor="@drawable/main_bottom_tab_text_color_selector"
android:text="@string/tabBottom_zhishitixi"
app:layout_constraintEnd_toStartOf="@+id/tablayout_rb_navigate"
app:layout_constraintHorizontal_bias="0.5"
android:gravity="center_horizontal"
app:layout_constraintStart_toEndOf="@+id/tablayout_rb_home"
app:layout_constraintTop_toTopOf="@+id/tablayout_rb_home" />
<RadioButton
android:id="@+id/tablayout_rb_navigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableTop="@drawable/ic_navigation_black_24dp"
android:textColor="@drawable/main_bottom_tab_text_color_selector"
android:text="@string/tabBottom_daohang"
app:layout_constraintEnd_toStartOf="@+id/tablayout_rb_project"
app:layout_constraintHorizontal_bias="0.5"
android:gravity="center_horizontal"
app:layout_constraintStart_toEndOf="@+id/tablayout_rb_knowledge"
app:layout_constraintTop_toTopOf="@+id/tablayout_rb_knowledge" />
<RadioButton
android:id="@+id/tablayout_rb_project"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableTop="@drawable/ic_project_black_24dp"
android:text="@string/tabBottom_xiangmu"
android:gravity="center_horizontal"
android:textColor="@drawable/main_bottom_tab_text_color_selector"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/tablayout_rb_navigate"
app:layout_constraintTop_toTopOf="@+id/tablayout_rb_navigate" />
<ImageView
android:id="@+id/imageView2"
style="@style/WanAndroid.split_line_1"
android:layout_height="1dp"
android:layout_marginBottom="4dp"
app:layout_constraintBottom_toTopOf="@+id/tablayout_rb_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</com.c03.jy.wanandroid.playandroid.view.TabBottomLayout >
imageView 的stytle
<style name="WanAndroid.split_line_1">
<item name="android:layout_height">1dp</item>
<item name="android:background">@color/color_c</item>
</style>
imageVIew 是横线
java代码
自定义类继承 ConstraintLayout
public class TabBottomLayout extends ConstraintLayout {
public TabBottomLayout(Context context) {
super(context);
}
public TabBottomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TabBottomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
RadioButton radioButton;
for(int i = 0; i < getChildCount(); i++){
final View v = getChildAt(i);
if(v instanceof RadioButton){
radioButton = (RadioButton) v;
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 因为我们设置的是 选中状态的监听,因此 每当我们点击一个 radioButton 时 这个方法会调用
// 两次,第一次是我们点击的那个radiobutton 变为选中,另一次是上次选中的那个有原来的选中变为未选中
if(isChecked){
unCheckOther(buttonView);
if(onCheckedChangeListener != null){
onCheckedChangeListener.onCheckedChanged(TabBottomLayout.this, (RadioButton) v);
}
}
}
});
}
}
}
public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener){
this.onCheckedChangeListener=onCheckedChangeListener;
}
public void check(int radioButtonid){
View viewById = findViewById(radioButtonid);
if (viewById!=null){
((RadioButton)viewById).setChecked(true);
unCheckOther(viewById);
}
}
//取消其他Radiobutton 的选中状态
private void unCheckOther(View v){
RadioButton r;
View view;
for (int i = 0; i <getChildCount() ; i++) {
view=getChildAt(i);
if (view instanceof RadioButton){
r= (RadioButton) view;
if (v!=r){
r.setChecked(false);
}
}
}
}
private OnCheckedChangeListener onCheckedChangeListener;
public interface OnCheckedChangeListener{
void onCheckedChanged(TabBottomLayout group, RadioButton checkedView);
}
}
布局引用
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".NavigationActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_navigation"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
引用 否则找不到控件
</android.support.design.widget.CoordinatorLayout>
使用 id为布局ID
private void initView() {
TabBottomLayout mBottomTabLayout = findViewById(R.id.main_bottom_tab_layout);
mBottomTabLayout.setOnCheckedChangeListener(new TabBottomLayout.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(TabBottomLayout group, RadioButton checkedView) {
int id = checkedView.getId();
switch (id){
case R.id.tablayout_rb_home:{
addFrament(HomeFragment.class, R.id.framelayout);
}
break;
case R.id.tablayout_rb_knowledge:{
addFrament(KnowlegdeFragment.class, R.id.framelayout);
}
break;
case R.id.tablayout_rb_navigate:{
addFrament(NavigateFragment.class, R.id.framelayout);
}
break;
case R.id.tablayout_rb_project:{
addFrament(ProjectFragment.class, R.id.framelayout);
}
break;
}
}
});
mBottomTabLayout.check(R.id.tablayout_rb_home);
}