展会信息港展会大全

[Android学UI之]实现分断Button,模仿MIUI设置页面顶部Button
来源:互联网   发布日期:2015-12-07 15:31:30   浏览:2038次  

导读:功能: 拼接的Button。使用说明: 用RidaoGroup包裹几个RidaoButton,实现拼接。还是看图,更真实!!!甀折媗葐比较简单,这个功能也不太难。...

功能:

拼接的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>

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港