第一部分 自定义布局
public class BottomLayout extends LinearLayout {
private int normalIcon;
private int focusIcon;
private boolean isFocused=false;
private ImageView ivIcon;
private TextView tvText;
public BottomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//加载布局文件,与setContentView()效果一样
LayoutInflater.from(context).inflate(R.layout.act_main_bottom_layout, this);//控件布局
ivIcon = (ImageView) findViewById(R.id.iv_main_bottom_icon);
tvText = (TextView) findViewById(R.id.tv_main_bottom_text);
tvText.setTextColor(Color.WHITE);//文字默认颜色
}
public void setNormalIcon(int normalIcon) {
this.normalIcon = normalIcon;
ivIcon.setImageResource(normalIcon);
}
//为按钮设置图片
public void setFocusIcon(int focusIcon) {
this.focusIcon = focusIcon;
}
public void setIconText(String text) {
tvText.setText(text);
}
public void setFocused(boolean isFocused) {
//如果已经处在设置的状态中,就不进行操作
if (this.isFocused != isFocused) {
this.isFocused = isFocused;
if (isFocused) {
//设置获得焦点后的图片
//文字加粗
//文字颜色
ivIcon.setImageResource(focusIcon);
tvText.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
tvText.setTextColor(Color.BLACK);
} else {
//设置获得普通状态的图片
//文字不加粗
//文字颜色
ivIcon.setImageResource(normalIcon);
tvText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
tvText.setTextColor(Color.WHITE);
}
}
}
}
第二部分 自定义布局中的layout文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="5dp"
android:background="#eae17b">
<ImageView
android:id="@+id/iv_main_bottom_icon"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
tools:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_main_bottom_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_blue_dark"
android:textSize="10sp"
tools:text="icon"/>
</LinearLayout>
第三部分 Activity部分
public class MainActivity extends Activity implements View.OnClickListener {
BottomLayout blSituation;
BottomLayout blMap;
BottomLayout blDiscover;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
//初始化控件
initView();
}
private void initView() {
initBottomLayout();
}
private void initBottomLayout() {
blSituation = (BottomLayout) findViewById(R.id.bottom_icon_situation);
blMap = (BottomLayout) findViewById(R.id.bottom_icon_map);
blDiscover = (BottomLayout) findViewById(R.id.bottom_icon_discover);
blSituation.setNormalIcon(R.drawable.bottom_icon_situation_normal);
blSituation.setFocusIcon(R.drawable.bottom_icon_situation_focus);
blSituation.setIconText("首页");
blSituation.setFocused(true);
blSituation.setOnClickListener(this);
blMap.setNormalIcon(R.drawable.bottom_icon_map_normal);
blMap.setFocusIcon(R.drawable.bottom_icon_map_focus);
blMap.setIconText("发现");
blMap.setFocused(false);
blMap.setOnClickListener(this);
blDiscover.setNormalIcon(R.drawable.bottom_icon_discover_normal);
blDiscover.setFocusIcon(R.drawable.bottom_icon_discover_focus);
blDiscover.setIconText("个人中心");
blDiscover.setFocused(false);
blDiscover.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bottom_icon_situation:
blSituation.setFocused(true);
blMap.setFocused(false);
blDiscover.setFocused(false);
break;
case R.id.bottom_icon_map:
blSituation.setFocused(false);
blMap.setFocused(true);
blDiscover.setFocused(false);
break;
case R.id.bottom_icon_discover:
blSituation.setFocused(false);
blMap.setFocused(false);
blDiscover.setFocused(true);
break;
}
}
}
第四部分 Activity的布局部分
<?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"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="9" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<com.example.double2.mybottomlayout.BottomLayout
android:id="@+id/bottom_icon_situation"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"></com.example.double2.mybottomlayout.BottomLayout>
<com.example.double2.mybottomlayout.BottomLayout
android:id="@+id/bottom_icon_map"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</com.example.double2.mybottomlayout.BottomLayout>>
<com.example.double2.mybottomlayout.BottomLayout
android:id="@+id/bottom_icon_discover"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</com.example.double2.mybottomlayout.BottomLayout>>
</LinearLayout>
</LinearLayout>