仿投哪网底部切换Button效果

先上效果图:

仿投哪网底部切换Button效果_第1张图片


(1)clipChild属性用来定义其子控件是否要在他应有的边界内进行绘制。 默认情况下,clipChild被设置为true。

(2)layout_gravity控制超出的部分如何显示。


<?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:clipChildren="false"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#f5f5f5"
        android:layout_weight="1.0" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/bottom_btn_normal_height"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imgHome"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:scaleType="fitCenter"
            android:src="@mipmap/home" />

        <ImageView
            android:id="@+id/imgProduct"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:scaleType="fitCenter"
            android:src="@mipmap/product" />

        <ImageView
            android:id="@+id/imgMore"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_gravity="bottom"
            android:layout_weight="1.0"
            android:scaleType="fitCenter"
            android:src="@mipmap/more" />
    </LinearLayout>

</LinearLayout>

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView mImgHome;
    private ImageView mImgProduct;
    private ImageView mImgMore;

    private List<ImageView> imageViewList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImgHome = (ImageView) findViewById(R.id.imgHome);
        mImgProduct = (ImageView) findViewById(R.id.imgProduct);
        mImgMore = (ImageView) findViewById(R.id.imgMore);

        mImgHome.setOnClickListener(this);
        mImgProduct.setOnClickListener(this);
        mImgMore.setOnClickListener(this);

        imageViewList.add(mImgHome);
        imageViewList.add(mImgProduct);
        imageViewList.add(mImgMore);

        mImgHome.performClick();
    }

    @Override
    public void onClick(View v) {
        changeBottomBtnBackgroud(v);
    }

    private void changeBottomBtnBackgroud(View v) {
        for (int i = 0; i < imageViewList.size(); i++) {
            ImageView imageView = imageViewList.get(i);
            if (v == imageView) {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,(int)getResources().getDimension(R.dimen.bottom_btn_press_height), 1.0f);
                imageView.setLayoutParams(lp);
                // 注意需要设置这个属性,对应的xml中的属性为layout_gravity="bottom"
                lp.gravity = Gravity.BOTTOM;
                setBottomBtnBackgroud(imageView, true);
            } else {
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
                imageView.setLayoutParams(lp);
                setBottomBtnBackgroud(imageView, false);
            }
        }
    }

    private void setBottomBtnBackgroud(ImageView imageView, boolean isPress) {
        if (isPress) {
            if (imageView.getId() == R.id.imgHome) {
                imageView.setImageResource(R.mipmap.home_press);
            } else if (imageView.getId() == R.id.imgProduct) {
                imageView.setImageResource(R.mipmap.product_press);
            } else {
                imageView.setImageResource(R.mipmap.more_press);
            }
        } else {
            if (imageView.getId() == R.id.imgHome) {
                imageView.setImageResource(R.mipmap.home);
            } else if (imageView.getId() == R.id.imgProduct) {
                imageView.setImageResource(R.mipmap.product);
            } else {
                imageView.setImageResource(R.mipmap.more);
            }
        }
    }
}


demo下载地址:

https://github.com/robotlife/TouNaDemo

你可能感兴趣的:(仿投哪网底部切换Button效果)