[Android學UI之]實現分斷Button,模仿MIUI設定頁面頂部Button

來源:互聯網
上載者:User

功能:

    拼接的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/button_one" 
        android:gravity="top|center" 
        android:orientation="horizontal" > 
 
        <RadioButton 
            android:id="@id/button_one" 
            android:button="@null" 
            android:gravity="center" 
            android:minHeight="33dip" 
            android:minWidth="40dip" 
            android:text="我是第一個" 
            android:textAppearance="?android:attr/textAppearanceSmall" /> 
 
        <RadioButton 
            android:id="@+id/button_two" 
            android:button="@null" 
            android:gravity="center" 
            android:minHeight="33dip" 
            android:minWidth="40dip" 
            android:text="我是第二個" 
            android:textAppearance="?android:attr/textAppearanceSmall" /> 
 
        <RadioButton 
            android:id="@+id/button_three" 
            android:button="@null" 
            android:gravity="center" 
            android:minHeight="33dip" 
            android:minWidth="40dip" 
            android:text="我是第三個" 
            android:textAppearance="?android:attr/textAppearanceSmall" /> 
    </com.bbswp.topbuttondemo.view.SegmentedRadioGroup> 
 
    <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/button_one" 
        android:gravity="center_horizontal" 
        android:orientation="horizontal" > 
 
        <RadioButton 
            android:id="@+id/button_four" 
            android:button="@null" 
            android:gravity="center" 
            android:minHeight="33dip" 
            android:minWidth="40dip" 
            android:onClick="onClick" 
            android:text="我是單獨的一個" 
            android:textAppearance="?android:attr/textAppearanceSmall" /> 
    </com.bbswp.topbuttondemo.view.SegmentedRadioGroup> 
 
</LinearLayout> 


這裡還有重要的四個drawable檔案,用於按下的效果顯示:

segment_radio_button.xml
[html]
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_enabled="false"  android:drawable="@drawable/segment_grey" /> 
    <item android:state_focused="true" android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/segment_grey_focus" /> 
    <item android:state_focused="true" android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/segment_white_focus" /> 
     
    <item android:state_pressed="true"  android:drawable="@drawable/segment_grey_press" />  
    <item android:state_checked="false"  android:drawable="@drawable/segment_white" /> 
    <item android:drawable="@drawable/segment_grey" />  <!-- default  --> 
</selector> 


後面是左:

segment_radio_left.xml
[html] 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <item android:drawable="@drawable/segment_radio_grey_left_focus" android:state_checked="true" android:state_focused="true" android:state_pressed="false"/> 
    <item android:drawable="@drawable/segment_radio_white_left_focus" android:state_checked="false" android:state_focused="true" android:state_pressed="false"/> 
    <item android:drawable="@drawable/segment_radio_grey_left_press" android:state_pressed="true"/> 
    <item android:drawable="@drawable/segment_radio_white_left" android:state_checked="false"/> 
    <item android:drawable="@drawable/segment_radio_grey_left"/> 
 
</selector> 


中:

segment_radio_mid.xml
[html] 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_focused="true" android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/segment_radio_grey_middle_focus" /> 
    <item android:state_focused="true" android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/segment_radio_white_middle_focus" /> 
     
    <item android:state_pressed="true"  android:drawable="@drawable/segment_radio_grey_middle_press" />  
    <item android:state_checked="false" android:drawable="@drawable/segment_radio_white_middle" />  
    <item android:drawable="@drawable/segment_radio_grey_middle" />  
</selector>  www.2cto.com


右:

segment_radio_right.xml
[html] 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_focused="true" android:state_checked="true" android:state_pressed="false" android:drawable="@drawable/segment_radio_grey_right_focus" /> 
    <item android:state_focused="true" android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/segment_radio_white_right_focus" /> 
     
    <item android:state_pressed="true"  android:drawable="@drawable/segment_radio_grey_right_press" />  
    <item android:state_checked="false" android:drawable="@drawable/segment_radio_white_right" />    
    <item android:drawable="@drawable/segment_radio_grey_right" /> 
 
</selector> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.