Android自定义控件之extends textview
上效果图:
实现:
这个效果的实现有好多个方式,这里使用的是一个自定义状态的方式,咱们分三步走,代码如下:
1)自定义view,继承 TextView。当然对状态进行了自定义。
public class PseudoAwareTextView extends TextView {
private static final int[] STATE_ONLY_ONE = new int[] {
android.R.attr.state_first,
android.R.attr.state_last,
};
private static final int[] STATE_FIRST = new int[] {
android.R.attr.state_first
};
private static final int[] STATE_LAST = new int[] {
android.R.attr.state_last
};
public PseudoAwareTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
ViewGroup parent = (ViewGroup) getParent();
if (parent == null) {
return super.onCreateDrawableState(extraSpace);
}
final int size = parent.getChildCount();
final boolean isFirst = (parent.getChildAt(0) == this);
final boolean isLast = (parent.getChildAt(size-1) == this);
int[] states = super.onCreateDrawableState(extraSpace + 2);
if (isFirst && isLast) {
mergeDrawableStates(states, STATE_ONLY_ONE);
} else if (isFirst) {
mergeDrawableStates(states, STATE_FIRST);
} else if (isLast) {
mergeDrawableStates(states, STATE_LAST);
}
return states;
}
}
2)自定义不同状态下相对应的drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First and last -->
<item android:state_first="true" android:state_last="true"><shape>
<solid android:color="#63abed" />
<corners android:radius="10dp" />
</shape></item>
<!-- First -->
<item android:state_first="true"><shape>
<solid android:color="#63abed" />
<corners android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" />
</shape></item>
<!-- Last -->
<item android:state_last="true"><shape>
<solid android:color="#63abed" />
<corners android:bottomRightRadius="10dp" android:topRightRadius="10dp" />
</shape></item>
<!-- Default -->
<item><shape>
<solid android:color="#63abed" />
</shape></item>
</selector>
3)当然是在布局中使用,注意此时这几个view的父容器必须只是包含这几个自定义控件,不然效果会出错的。
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.androidtest.MainActivity" >
<com.example.androidtest.PseudoAwareTextView
android:id="@+id/pseudoAwareTextView1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/sss"
android:text="PseudoAwareTextView1" />
<com.example.androidtest.PseudoAwareTextView
android:id="@+id/pseudoAwareTextView2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/sss"
android:text="PseudoAwareTextView2" />
<com.example.androidtest.PseudoAwareTextView
android:id="@+id/pseudoAwareTextView3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/sss"
android:text="PseudoAwareTextView3" />
</LinearLayout>
到此为止,试试效果怎么样。