blog.csdn.net/qduningning/article/details/37935227
Solid填充色
效果图:
这里只简单做了两个按钮的。
首先是两个按钮的背景:
res/drawable/seg_left.xml
[html]view plaincopy
res/drawable/seg_right.xml
[html]view plaincopy
字体颜色:
res/drawable/seg_text_color_selector.xml
[html]view plaincopy
这几个是对选中状态进行设置。
下面对LinearLayout进行改造~~~
其实就是放两个TextView。
SegmentView.Java
[java]view plaincopy
packagecn.haiwan.app.widget;
importorg.xmlpull.v1.XmlPullParser;
importandroid.R.integer;
importandroid.content.Context;
importandroid.content.res.ColorStateList;
importandroid.util.AttributeSet;
importandroid.util.TypedValue;
importandroid.view.Gravity;
importandroid.view.View;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;
importcn.haiwan.R;
publicclassSegmentViewextendsLinearLayout {
privateTextView textView1;
privateTextView textView2;
privateonSegmentViewClickListener listener;
publicSegmentView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
publicSegmentView(Context context) {
super(context);
init();
}
privatevoidinit() {
// this.setLayoutParams(new LinearLayout.LayoutParams(dp2Px(getContext(), 60), LinearLayout.LayoutParams.WRAP_CONTENT));
textView1 =newTextView(getContext());
textView2 =newTextView(getContext());
textView1.setLayoutParams(newLayoutParams(0, LayoutParams.WRAP_CONTENT,1));
textView2.setLayoutParams(newLayoutParams(0, LayoutParams.WRAP_CONTENT,1));
textView1.setText("SEG1");
textView2.setText("SEG2");
XmlPullParser xrp = getResources().getXml(R.drawable.seg_text_color_selector);
try{
ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
textView1.setTextColor(csl);
textView2.setTextColor(csl);
}catch(Exception e) {
}
textView1.setGravity(Gravity.CENTER);
textView2.setGravity(Gravity.CENTER);
textView1.setPadding(3,6,3,6);
textView2.setPadding(3,6,3,6);
setSegmentTextSize(16);
textView1.setBackgroundResource(R.drawable.seg_left);
textView2.setBackgroundResource(R.drawable.seg_right);
textView1.setSelected(true);
this.removeAllViews();
this.addView(textView1);
this.addView(textView2);
this.invalidate();
textView1.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
if(textView1.isSelected()) {
return;
}
textView1.setSelected(true);
textView2.setSelected(false);
if(listener !=null) {
listener.onSegmentViewClick(textView1,0);
}
}
});
textView2.setOnClickListener(newOnClickListener() {
@Override
publicvoidonClick(View v) {
if(textView2.isSelected()) {
return;
}
textView2.setSelected(true);
textView1.setSelected(false);
if(listener !=null) {
listener.onSegmentViewClick(textView2,1);
}
}
});
}
/**
* 设置字体大小 单位dip
*
2014年7月18日
* @param dp
* @author RANDY.ZHANG
*/
publicvoidsetSegmentTextSize(intdp) {
textView1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, dp);
textView2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, dp);
}
privatestaticintdp2Px(Context context,floatdp) {
finalfloatscale = context.getResources().getDisplayMetrics().density;
return(int) (dp * scale +0.5f);
}
publicvoidsetOnSegmentViewClickListener(onSegmentViewClickListener listener) {
this.listener = listener;
}
/**
* 设置文字
*
2014年7月18日
* @param text
* @param position
* @author RANDY.ZHANG
*/
publicvoidsetSegmentText(CharSequence text,intposition) {
if(position ==0) {
textView1.setText(text);
}
if(position ==1) {
textView2.setText(text);
}
}
publicstaticinterfaceonSegmentViewClickListener{
/**
*
*
2014年7月18日
* @param v
* @param position 0-左边 1-右边
* @author RANDY.ZHANG
*/
publicvoidonSegmentViewClick(View v,intposition);
}
}
布局文件引用
[java]view plaincopy
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
网友评论