Android实现底部导航选中凸起效果

最近项目底部导航栏需要做成中间的条目选中凸起,未选中不凸起的效果,自己写了个,先贴出来代码先。。




    

    

        

            

            

        


        

            

            

        

        

            

            

        
    

package com.face.jfshare.androidplus;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.main_content)
    FrameLayout mainContent;
    @BindView(R.id.iv1)
    ImageView iv1;
    @BindView(R.id.rl1)
    RelativeLayout rl1;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.rl2)
    RelativeLayout rl2;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.rl3)
    RelativeLayout rl3;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;

    int preSelectedTabPosition = -1;
    int[] lightPics = {R.drawable.rb_home_pressed, R.drawable.dianxin, R.drawable.rb_home_pressed};
    int[] greyPics = {R.drawable.rb_home_pressed, R.drawable.rb_home_pressed, R.drawable.rb_home_pressed};
    List imageViews = new ArrayList<>();
    List textViews = new ArrayList<>();
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        imageViews.add(iv1);
        imageViews.add(iv2);
        imageViews.add(iv3);

        textViews.add(tv1);
        textViews.add(tv2);
        textViews.add(tv3);
    }

    public void setTextColor(List text, int position) {
        if (position != preSelectedTabPosition) {
            text.get(position).setTextColor(getResources().getColor(R.color.red));
            if (preSelectedTabPosition != -1)
                text.get(preSelectedTabPosition).setTextColor(getResources().getColor(R.color.black));
        }
    }

    public void setBackgroud(int position) {
        if (position != preSelectedTabPosition) {
            if (position == 1) {
                LinearLayout.LayoutParams params4rl = (LinearLayout.LayoutParams) rl2.getLayoutParams();
                params4rl.height = DensityUtils.dp2px(MainActivity.this, 150);
                rl2.setLayoutParams(params4rl);
            } else {
                LinearLayout.LayoutParams params4rl = (LinearLayout.LayoutParams) rl2.getLayoutParams();
                params4rl.height = ViewGroup.LayoutParams.MATCH_PARENT;
                rl2.setLayoutParams(params4rl);
            }
            imageViews.get(position).setImageDrawable(getResources().getDrawable(lightPics[position]));
            if (preSelectedTabPosition != -1)
                imageViews.get(preSelectedTabPosition).setImageDrawable(getResources().getDrawable(greyPics[preSelectedTabPosition]));
        }
    }

    public void setButton(int position) {
        setTextColor(textViews, position);
        setBackgroud(position);
//        mViewPager.setCurrentItem(position,false);
        preSelectedTabPosition = position;
    }

    @OnClick({R.id.rl1, R.id.rl2, R.id.rl3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl1:
                setButton(0);
                break;
            case R.id.rl2:
                setButton(1);
                break;
            case R.id.rl3:
                setButton(2);
                break;
        }
    }
}

你可能感兴趣的:(Android实现底部导航选中凸起效果)