Android开发之屏幕适配的自定义矩形导航栏

来源:互联网 发布:小米关闭自动更新软件 编辑:程序博客网 时间:2024/06/07 02:26

1、效果预览:




2、布局文件:

    <RadioGroup        android:id="@+id/radioGroup"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:layout_marginTop="50dp"        android:background="@color/gray"        android:orientation="horizontal" >        <RadioButton            android:id="@+id/rb1"            android:layout_width="match_parent"            android:layout_weight="1"            android:layout_height="wrap_content"            android:layout_margin="0.5dp"            android:background="@color/gray"            android:button="@null"            android:gravity="center"            android:text="tab1"            android:textColor="@color/white" />        <RadioButton            android:id="@+id/rb2"            android:layout_width="match_parent"            android:layout_weight="1"            android:layout_height="wrap_content"            android:layout_margin="0.5dp"            android:background="@color/white"            android:button="@null"            android:gravity="center"            android:text="tab2"            android:textColor="@color/gray" />        <RadioButton            android:id="@+id/rb3"            android:layout_width="match_parent"            android:layout_weight="1"            android:layout_height="wrap_content"            android:layout_margin="0.5dp"            android:background="@color/white"            android:button="@null"            android:gravity="center"            android:text="tab3"            android:textColor="@color/gray" />    </RadioGroup>

1、在RadioGroup中,设置background为gray
2、在RadioButton中,利用weight进行平均分配,并且设置layout_margin为0.5dp,这个时候会显示出矩形的边框
3、在RadioButton中,设置自己的background和textColor


3、成员变量的声明

    // 导航栏的条目数    public static int BAR_NUMBER = 3;    // 导航栏到父容器的偏移值    public static int SCREEN_OFFSET = 20;        //控件      private RadioGroup radioGroup = null;    private RadioButton[] radioButtons = new RadioButton[BAR_NUMBER];        //控件的参数    private DisplayMetrics dm = null;


4、初始化布局

    /**     * 初始化布局     * @author chenchen  2014-11-20     */    private void initialView() {            radioGroup = (RadioGroup) findViewById(R.id.radioGroup);        radioButtons[0] = (RadioButton) findViewById(R.id.rb1);        radioButtons[1] = (RadioButton) findViewById(R.id.rb2);        radioButtons[2] = (RadioButton) findViewById(R.id.rb3);    } 


5、屏幕的适配

    /**     * 完成控件的屏幕适配     * @author chenchen  2014-11-20     */    private void initialParams() {        //初始化屏幕参数        dm = getDM();              //得到屏幕的宽度        int screen_width = dm.widthPixels;        //设置矩形导航栏的宽度        ViewGroup.LayoutParams param_rg = radioGroup.getLayoutParams();        param_rg.width = screen_width - 2 * SCREEN_OFFSET ;        radioGroup.setLayoutParams(param_rg);    }

    /**     * @return 屏幕参数     * @author chenchen  2014-11-20     */    public DisplayMetrics getDM() {        DisplayMetrics dm = new DisplayMetrics();        this.getWindowManager().getDefaultDisplay().getMetrics(dm);        return dm;    }


6、设置监听器

    /**     * 给按钮添加监听器     * @author chenchen  2014-11-20     */    private void addListener() {        btns[0].setOnClickListener(this);        btns[1].setOnClickListener(this);        btns[2].setOnClickListener(this);                radioGroup.setOnCheckedChangeListener(this);    }

    @Override    public void onCheckedChanged(RadioGroup group, int checkedId) {        // 得到点击项的索引        int selectIndex = 0;        if (checkedId == R.id.rb1) {            selectIndex = 0;        } else if (checkedId == R.id.rb2) {            selectIndex = 1;        } else if (checkedId == R.id.rb3) {            selectIndex = 2;        }        // 改变所有Button的状态        for (int i = 0; i < BAR_NUMBER; i++) {            if (i == selectIndex) {                setSelectState(radioButtons[i], true);            } else {                setSelectState(radioButtons[i], false);            }        }    }



7、改变按钮的显示状态

</pre><pre name="code" class="java">    /**     * 设置Button的显示状态,设置背景颜色和字体颜色     * @param button     * @param isSelected     * @author chenchen  2014-11-20     */    private void setSelectState(RadioButton button, boolean isSelected) {        if (isSelected) {            button.setBackgroundColor(getResources().getColor(R.color.gray));            button.setTextColor(getResources().getColor(R.color.white));        } else {            button.setBackgroundColor(getResources().getColor(R.color.white));            button.setTextColor(getResources().getColor(R.color.gray));        }    }



0 0
原创粉丝点击