中间一期项目开发中遇到了三个Button在一起的UI设计效果,之前第一手解决方案是RelativeLayout中嵌套Button,在Button之间放置竖线来实现这种效果,如图所示:
布局代码如下:
<LinearLayout
style="@style/StyleContentsSearchPriceLinear"
android:layout_toLeftOf="@id/PosMainLeftModuleAddBtn" >
<Button
android:id="@+id/PosMainLeftModuleCutBtn"
style="@style/StyleContentsLinearOneBtn" />
<TextView style="@style/StyleContentHLineText" />
<Button
android:id="@+id/PosMainLeftModuleSearchBtn"
style="@style/StyleContentsLinearTwoBtn" />
</LinearLayout>
很累赘的样式,但是多少效果也是大致实现了,但是还有个坑爹的地方是,那种效果你必须要到Java中进行声明并去改变,还好只有两个,万一哪天UI大婶给你来十来个,嘿嘿...
以上方式不建议使用,毕竟不符合Android轻量化的开发模式,对于后期维护也是比较坑组织的选择。下面介绍一个连体Button,GitHub上面的开源项目,具体地址找不到了,我了个去,这智商...
按照惯例先放置效果图:
上面有两种效果,第一种是文字样式的三个在一起的Button,第二个是图片样式的连体Button。首先来介绍第一种样式的吧,连体Button通过RedioButton来实现的:
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(com.makeramen.segmented.R.drawable.segment_radio_left);
for(int i=1; i < count-1; i++){
super.getChildAt(i).setBackgroundResource(com.makeramen.segmented.R.drawable.segment_radio_middle);
}
super.getChildAt(count-1).setBackgroundResource(com.makeramen.segmented.R.drawable.segment_radio_right);
}else if (count == 1){
super.getChildAt(0).setBackgroundResource(com.makeramen.segmented.R.drawable.segment_button);
}
}
}
之前的博客写过一个自定义换行的自定义大小布局样式,看这个毫无压力吧,changeButtonsImages()方法看懂就OK了。
有了自定义RedioGroup就可以来写XML布局样式了:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.makeramen.segmented.SegmentedRadioGroup
android:id="@+id/segment_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:checkedButton="@+id/button_one"
android:orientation="horizontal" >
<RadioButton
android:id="@id/button_one"
android:button="@null"
android:gravity="center"
android:minHeight="33dip"
android:minWidth="40dip"
android:text="One"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/radio_colors" />
<RadioButton
android:id="@+id/button_two"
android:button="@null"
android:gravity="center"
android:minHeight="33dip"
android:minWidth="40dip"
android:text="Two"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/radio_colors" />
<RadioButton
android:id="@+id/button_three"
android:button="@null"
android:gravity="center"
android:minHeight="33dip"
android:minWidth="40dip"
android:text="Three"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/radio_colors" />
</com.makeramen.segmented.SegmentedRadioGroup>
</LinearLayout>
Activity实现效果:
public class SegmentedRadioActivity extends Activity implements OnCheckedChangeListener {
SegmentedRadioGroup segmentText;
Toast mToast;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
segmentText = (SegmentedRadioGroup) findViewById(R.id.segment_text);
segmentText.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (group == segmentText) {
if (checkedId == R.id.button_one) {
mToast.setText("One");
mToast.show();
} else if (checkedId == R.id.button_two) {
mToast.setText("Two");
mToast.show();
} else if (checkedId == R.id.button_three) {
mToast.setText("Three");
mToast.show();
}
}
}
}
查工作的来了,闪,稍后继续!!!