当前位置:操作系统 > 安卓/Android >>

[Android学UI之]实现分断Button,模仿MIUI设置页面顶部Button

功能:

    拼接的Button。


使用说明:

    用RidaoGroup包裹几个RidaoButton,实现拼接。


还是看图,更真实!!!

 

页面做的比较简单,这个功能也不太难。。这只是其中的实现方式之一。有其它更好的方式,请告之。


下面还是看代码吧:

界面Activity:

[html] 
package com.bbswp.topbuttondemo; 
 
import com.bbswp.topbuttondemo.view.SegmentedRadioGroup; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
import android.widget.Toast; 
 
public class MainActivity extends Activity implements OnCheckedChangeListener { 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
 
        SegmentedRadioGroup group = (SegmentedRadioGroup) findViewById(R.id.segment_text); 
        group.setOnCheckedChangeListener(this); 
 
    } 
 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
        switch (checkedId) { 
            case R.id.button_one: 
                toast(1); 
                break; 
 
            case R.id.button_two: 
                toast(2); 
                break; 
 
            case R.id.button_three: 
                toast(3); 
                break; 
 
            default: 
                break; 
        } 
 
    } 
 
    public void onClick(View view) { 
        toast(4); 
    } 
 
    private void toast(int id) { 
        Toast.makeText(this, "点击:" + id, 0).show(); 
    } 
 

 

自定义一个View,用于包裹RidaoButton.


看代码:

[html] 
/* 
 * Copyright (C) 2011 Make Ramen, LLC 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
 
package com.bbswp.topbuttondemo.view; 
 
import com.bbswp.topbuttondemo.R; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.RadioGroup; 
 
public class SegmentedRadioGroup extends RadioGroup { 
 
    public SegmentedRadioGroup(Context context) { 
        super(context); 
    } 
 
    public SegmentedRadioGroup(Context context, AttributeSet attrs) { 
        super(context, attrs); 
    } 
 
    @Override 
    protected void onFinishInflate() { 
        super.onFinishInflate(); 
        changeButtonsImages(); 
    } 
 
    private void changeButtonsImages(){ 
        int count = super.getChildCount(); 
 
        if(count > 1){ 
            super.getChildAt(0).setBackgroundResource(R.drawable.segment_radio_left); 
            for(int i=1; i < count-1; i++){ 
                super.getChildAt(i).setBackgroundResource(R.drawable.segment_radio_mid); 
            } 
            super.getChildAt(count-1).setBackgroundResource(R.drawable.segment_radio_right); 
        }else if (count == 1){ 
            super.getChildAt(0).setBackgroundResource(R.drawable.segment_radio_button); 
        } 
    } 

 

界面少不了xml。。。看看吧:
 
<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" 
    tools:context=".MainActivity" > 
 
    <com.bbswp.topbuttondemo.view.SegmentedRadioGroup 
        android:id="@+id/segment_text" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dip" 
        android:checkedButton="@+id/butto

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,