Android菜鳥練習第三十一課 下方icon切換效果的自定義佈局

第一部分 自定義佈局

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章