做一个圆角*Button组件,点击Button背景变亮*
1.在res/drawable/login.xml自定义背景选择器
2.在res/layout/point.xml创建一个Button组件
//drawable/login.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape >
<corners android:radius="50dp"/>
<solid android:color="#00A5A5" />
</shape>
</item>
<item android:state_pressed="false">
<shape>
<corners android:radius="50dp"/>
<solid android:color="#336699" />
</shape>
</item>
</selector>
//point.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:padding="10dp"
android:text="登录"
android:gravity="center"
android:background="@drawable/login"
android:textColor="@android:color/white"
android:textSize="18sp"/>
做一个滑动界面下的三个点
1.在res/drawable/point.xml自定义圆形背景选择器
2.在res/layout/point.xml创建三个View
3.在MainActivity中设置定时器,循环设置改变状态
//在drawable/point.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true">
<shape android:shape="oval">
<solid android:color="#fff"/>
</shape>
</item>
<item android:state_selected="false">
<shape android:shape="oval">
<solid android:color="#6ccc"/>
</shape>
</item>
</selector>
//在layout/point.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:background="@android:color/black" >
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:background="@drawable/point" />
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:background="@drawable/point" />
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:background="@drawable/point" />
</LinearLayout>
在MainActivity中
LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.ll);
ll.getChildAt(0).setSelected(true);
new Timer().schedule(new TimerTask() {
int index = 0;
@Override
// 子线程不允许修改UI
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
ll.getChildAt(index % 3).setSelected(false);
index++;
ll.getChildAt(index % 3).setSelected(true);
}
});
}
}, 1000, 100);
}
做一个圆角RadioButton,默认背景色是浅蓝色,字体颜色是黑色,选中之后背景色是黑色,字体颜色是白色
1.在res/color/one_of_two.xml自定义字体颜色选择器
2.在res/drawable/button_left.xml中自定义左边背景颜色选择器
3.在res/drawable/button_right.xml中自定义右边背景颜色选择器
4.在res/layout/one_of_two中创建两个选择Button
5.在src/MainActivity中默认设置左边Button是被选中状态
1.在res/color/one_of_two.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@android:color/white"/>
<item android:state_checked="false" android:color="#369"/>
</selector>
2.在res/drawable/button_left.xml中
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true">
<shape >
<corners android:topLeftRadius="50dp"
android:bottomLeftRadius="50dp"/>
<solid android:color="#383838" />
</shape>
</item>
<item android:state_checked="false">
<shape >
<corners android:topLeftRadius="50dp"
android:bottomLeftRadius="50dp"/>
<solid android:color="#AEEEEE" />
</shape>
</item>
</selector>
3.在res/drawable/button_right.xml中
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true">
<shape >
<corners android:topRightRadius="50dp"
android:bottomRightRadius="50dp"/>
<solid android:color="#383838" />
</shape>
</item>
<item android:state_checked="false">
<shape >
<corners android:bottomRightRadius="50dp"
android:topRightRadius="50dp"/>
<solid android:color="#AEEEEE" />
</shape>
</item>
</selector>
4.在res/layout/one_of_two中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal" >
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_left"
android:button="@null"
android:paddingBottom="10dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="10dp"
android:text="热门"
android:textColor="@color/one_of_two"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb_foc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_right"
android:button="@null"
android:paddingBottom="10dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="10dp"
android:text="关注"
android:textColor="@color/one_of_two"
android:textSize="18sp" />
</RadioGroup>
</LinearLayout>
5.在src/MainActivity中
RadioButton rb_hot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb_hot = (RadioButton) findViewById(R.id.rb_hot);
rb_hot.setChecked(true);
}