android實現對導航Tab設定底線選中效果

來源:互聯網
上載者:User

標籤:android   android開發   

直接看效果。此linearLayout只有兩個Button ,當選中Button1,Button1有個底線選中效果。當選中Buton2,Button2有個底線選中效果。

如。

 

package com.daoge.ui;import roboguice.activity.RoboActivity;import roboguice.inject.InjectView;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.LinearLayout;import com.daoge.utils.DensityUtils;import com.daoge.utils.ResourceReader;import com.roboguicetest.R;public class MainActivity extends RoboActivity implements OnClickListener {    @InjectView(R.id.btnTab001)    Button btnTab001;    @InjectView(R.id.btnTab002)    Button btnTab002;    @InjectView(R.id.layout_tab)    LinearLayout layout_tab;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.test_layout);        initAllViews();    }    private void initAllViews() {        btnTab001.setOnClickListener(this);        btnTab002.setOnClickListener(this);    }    public void onClick(View v) {        switch (v.getId()) {            case R.id.btnTab001 :                setTabSelected(btnTab001);                break;            case R.id.btnTab002 :                setTabSelected(btnTab002);                break;            default :                break;        }    }    private void setTabSelected(Button btnSelected) {        Drawable selectedDrawable = ResourceReader.readDrawable(this, R.drawable.shape_nav_indicator);        int screenWidth = DensityUtils.getScreenSize(MainActivity.this)[0];        int right = screenWidth / 2;        selectedDrawable.setBounds(0, 0, right, DensityUtils.dipTopx(this, 3));        btnSelected.setSelected(true);        btnSelected.setCompoundDrawables(null, null, null, selectedDrawable);        int size = layout_tab.getChildCount();        for (int i = 0; i < size; i++) {            if (btnSelected.getId() != layout_tab.getChildAt(i).getId()) {                layout_tab.getChildAt(i).setSelected(false);                ((Button) layout_tab.getChildAt(i)).setCompoundDrawables(null, null, null, null);            }        }    }}

最重要的代碼在這裡 :

private void setTabSelected(Button btnSelected) {        Drawable selectedDrawable = ResourceReader.readDrawable(this, R.drawable.shape_nav_indicator);        int screenWidth = DensityUtils.getScreenSize(MainActivity.this)[0];        int right = screenWidth / 2;        selectedDrawable.setBounds(0, 0, right, DensityUtils.dipTopx(this, 3));        btnSelected.setSelected(true);        btnSelected.setCompoundDrawables(null, null, null, selectedDrawable);        int size = layout_tab.getChildCount();        for (int i = 0; i < size; i++) {            if (btnSelected.getId() != layout_tab.getChildAt(i).getId()) {                layout_tab.getChildAt(i).setSelected(false);                ((Button) layout_tab.getChildAt(i)).setCompoundDrawables(null, null, null, null);            }        }    }

得到這個Drawable,那麼這個Drawable從哪裡來呢??

R.drawable.shape_nav_indicator

看這個drawable.shape檔案吧

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >    <solid android:color="@color/global_red" />    <size android:width="1000dp" />    <padding        android:bottom="2dp"        android:left="5dp"        android:right="5dp"        android:top="2dp" /></shape>

該Demo:http://pan.baidu.com/s/1jGGG0gm

其他精彩文章文章

在 android dialog中使用Autocompletetext 
大型網站架構設計-Solr
mysql雜湊索引
android學習筆記(32)網格視圖(GridView )和圖形切換器(ImageSwi...
android學習筆記(31)可展開的列表組件(ExpandableListView )

更多關於android開發文章


android實現對導航Tab設定底線選中效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.