android漂亮的按钮
A. 如何自定义android Button样式
方法/步骤
1
在layout中添加2个按钮,从下图中可以看出在按钮中调用了style和android:background属性,这两个属性一个是自定义样式,一个是给按钮添加背景图片,下面详细介绍下
2
展开res目录,可以看到在values目录下有styles.xml文件,该文件用于自定义样式,双击打开
3
下图中标注的是我自定义的样式,name为BtnStyle,当按钮调用自定义样式的时候访问这个name
4
下图是在button中调用自定义样式的方法,比较简单
5
下面分享下如何往按钮中添加自定义图片,使按钮看起来更漂亮些,因不同手机分辨率不同,那必然牵扯到图片的拉伸,在android系统下有个很好的技术“九宫格“,可以对图片进行处理,只对局部进行拉伸,给工具目录存储在android\sdk\tools\draw9patch.bat,经过该工具处理的图片以.9.png结尾,放到drawable文件夹中
6
下图是在Button中通过android:background属性加载图片的方法,至此我们自定义的按钮样式也就完成了,当然这只是个引子,在具体的项目工程中实现的效果要比这个demo复杂很多,有好的设计思路欢迎交流。
B. android:大伙讨论下:20个按钮大小 怎么排放 会显得精美点
球形,3D列表
C. 求 android RadioButton属性详解
一: 单选按钮RadioButton在Android平台上也应用的非常多,比如一些选择项的时候,会用到单选按钮,实现单选按钮由两部分组成,也就是RadioButton和RadioGroup配合使用
RadioButton的单选按钮;
RadioGroup是单选组合框,用于将RadioButton框起来;
在没有RadioGroup的情况下,RadioButton可以全部都选中;
当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个;
注意:单选按钮的事件监听用setOnCheckedChangeListener来对单选按钮进行监听
例子:一道选择题,选择哪个城市美女最多,当然,这个就是为了测试
java代码
1.package org.loulijun.radio;
2.
3.import android.app.Activity;
4.import android.os.Bundle;
5.import android.view.Gravity;
6.import android.widget.RadioButton;
7.import android.widget.RadioGroup;
8.import android.widget.TextView;
9.import android.widget.Toast;
10.
11.public class RadioTest extends Activity {
12. /** Called when the activity is first created. */
13. TextView textview;
14. RadioGroup radiogroup;
15. RadioButton radio1,radio2,radio3,radio4;
16. @Override
17. public void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.main);
20. textview=(TextView)findViewById(R.id.textview1);
21. radiogroup=(RadioGroup)findViewById(R.id.radiogroup1);
22. radio1=(RadioButton)findViewById(R.id.radiobutton1);
23. radio2=(RadioButton)findViewById(R.id.radiobutton2);
24. radio3=(RadioButton)findViewById(R.id.radiobutton3);
25. radio4=(RadioButton)findViewById(R.id.radiobutton4);
26.
27. radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
28.
29. @Override
30. public void onCheckedChanged(RadioGroup group, int checkedId) {
31. // TODO Auto-generated method stub
32. if(checkedId==radio2.getId())
33. {
34. DisplayToast("正确答案:"+radio2.getText()+",恭喜你,回答正确!");
35. }else
36. {
37. DisplayToast("请注意,回答错误!");
38. }
39. }
40. });
41. }
42. public void DisplayToast(String str)
43. {
44. Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
45. toast.setGravity(Gravity.TOP,0,220);
46. toast.show();
47. }
48.}
strings.xml文件
Xml代码
1.<?xml version="1.0" encoding="utf-8"?>
2.<resources>
3. <string name="hello">哪个城市美女多?</string>
4. <string name="app_name">单选按钮测试</string>
5. <string name="radiobutton1">杭州</string>
6. <string name="radiobutton2">成都</string>
7. <string name="radiobutton3">重庆</string>
8. <string name="radiobutton4">苏州</string>
9.</resources>
main.xml文件:注意,这里面,4个RadioButton包含在RadioGroup中
Xml代码
1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:orientation="vertical"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. >
7.<TextView
8. android:layout_width="fill_parent"
9. android:layout_height="wrap_content"
10. android:text="@string/hello"
11. android:id="@+id/textview1"
12. />
13. <RadioGroup
14. android:id="@+id/radiogroup1"
15. android:layout_width="wrap_content"
16. android:layout_height="wrap_content"
17. android:orientation="vertical"
18. android:layout_x="3px"
19. >
20. <RadioButton
21. android:id="@+id/radiobutton1"
22. android:layout_width="wrap_content"
23. android:layout_height="wrap_content"
24. android:text="@string/radiobutton1"
25. />
26. <RadioButton
27. android:id="@+id/radiobutton2"
28. android:layout_width="wrap_content"
29. android:layout_height="wrap_content"
30. android:text="@string/radiobutton2"
31. />
32. <RadioButton
33. android:id="@+id/radiobutton3"
34. android:layout_width="wrap_content"
35. android:layout_height="wrap_content"
36. android:text="@string/radiobutton3"
37. />
38. <RadioButton
39. android:id="@+id/radiobutton4"
40. android:layout_width="wrap_content"
41. android:layout_height="wrap_content"
42. android:text="@string/radiobutton4"
43. />
44. </RadioGroup>
45.</LinearLayout>
二:Android 自定义RadioButton的样式(和上面关系不大)
我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式。
最近用到RadioButton,利用xml修改android:background="@drawable/button_drawable",其中button_drawable为自己定义的.xml文件(res/drawable文件下),但是不成功,到网上查找,也没有正确的说法,我就开始自己尝试,最后做好了。
其实方法很简单,同样在res/drawable新建radiobutton.xml如下
Xml代码
1.<selector xmlns:android="http://schemas.android.com/apk/res/android">
2.
3. <item
4.
5.
6. android:state_enabled="true"
7.
8. android:state_checked="true"
9.
10. android:drawable="@drawable/check" />
11.
12. <item
13.
14. android:state_enabled="true"
15.
16. android:state_checked="false"
17.
18. android:drawable="@drawable/checknull" />
19.
20. </selector>
check和checknull分别为选中和位选中的图片。
然后在你的布局文件中,RadioButton 布局
设置android:button = "@drawable/radiobutton",就可以了!
D. android按钮样式
关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法。
首先android的selector是在drawable/xxx.xml中配置的。
先看一下listview中的状态:
把下面的XML文件保存成你自己命名的.xml文件(比如http://www.tiecou.com/),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片-->
<item android:drawable="@drawable/pic1" />
<!-- 没有焦点时的背景图片-->
<item android:state_window_focused="false" android:drawable="@drawable/pic1" />
<!-- 非触摸模式下获得焦点并单击时的背景图片-->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/pic2" />
<!-- 触摸模式下单击时的背景图片-->
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/pic3" />
<!--选中时的图片背景-->
<item android:state_selected="true" android:drawable="@drawable/pic4" />
<!--获得焦点时的图片背景-->
<item android:state_focused="true" android:drawable="@drawable/pic5" />
</selector>
使用些xml文件:第一种是在listview中配置android:listSelector="@drawable/list_item_bg"
或者在listview的item中添加属性android:background=“@drawable/list_item_bg"即可实现,或者在java代码中使用:Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);
ListView.setSelector(drawable);同样的效果。
但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"
使其透明。
其次再来看看Button的一些背景效果:
android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。
以下就是配置button中的文字效果:
drawable/button_font.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#FFF" />
<item android:state_focused="true" android:color="#FFF" />
<item android:state_pressed="true" android:color="#FFF" />
<item android:color="#000" />
</selector>
Button还可以实现更复杂的效果,例如渐变啊等等。
drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<!-- 定义当button 处于pressed 状态时的形态。-->
<shape>
<gradient android:startColor="#8600ff" />
<stroke android:width="2dp" android:color="#000000" />
<corners android:radius="5dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
<item android:state_focused="true">
<!-- 定义当button获得focus时的形态-->
<shape>
<gradient android:startColor="#eac100" />
<stroke android:width="2dp" android:color="#333333" color="#ffffff" />
<corners android:radius="8dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
</selector>
最后,需要在包含button的xml文件里添加两项。假如是main.xml 文件,我们需要在<Button />里加两项。
android:focusable="true"
android:backgroud="@drawable/button_color"
这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊
E. 32个实用酷炫的Android开源UI框架
1.Side-Menu.Android
分类侧滑菜单 , Yalantis 出品。
项目地址: https://github.com/Yalantis/Side-Menu.Android
2.Context-Menu.Android
可以方便快速集成漂亮带有动画效果的上下文菜单, Yalantis 出品。
项目地址: https://github.com/Yalantis/Context-Menu.Android
3.Pull-to-Refresh.Rentals-Android
提供一个简单可以自定义的 下拉刷新 实现,Yalantis 出品。
项目地址: https://github.com/Yalantis/Pull-to-Refresh.Rentals-Android
4.Titanic
可以显示水位上升下降的TextView
项目地址: https://github.com/RomainPiel/Titanic
5.AndroidSwipeLayout
滑动Layout ,支持单个View,ListView,GridView
项目地址: https://github.com/daimajia/AndroidSwipeLayout
Demo地址: Download Demo
6.Android Typeface Helper
可以帮你轻松实现自定义字体的库
项目地址: https://github.com/norbsoft/android-typeface-helper
7.android-lockpattern
Android的图案密码解锁
项目地址: https://code.google.com/p/android-lockpattern/
Demo地址: https://play.google.com/store/apps/details?id=group.pals.android.lib.ui.lockpattern.demo
文档介绍: https://code.google.com/p/android-lockpattern/wiki/QuickUse
APP示例:Android开机的图案密码解锁,支付宝的密码解锁
8.ToggleButton
状态切换的 Button,类似 iOS,用 View 实现
项目地址: https://github.com/zcweng/ToggleButton
9.WilliamChart
绘制图表的库,支持LineChartView、BarChartView和StackBarChartView三中图表类型,并且支持 Android 2.2及以上的系统。
项目地址: https://github.com/diogobernardino/WilliamChart
Demo地址: https://play.google.com/store/apps/details?id=com.db.williamchartdemo
Demo项目: https://github.com/diogobernardino/WilliamChart/tree/master/sample
10.实现滑动ViewPager渐变背景色
项目地址: https://github.com/TaurusXi/GuideBackgroundColorAnimation
11.Euclid
用户简历界面, Yalantis 出品。
项目地址: https://github.com/Yalantis/Euclid
12. InstaMaterial
Instagram的一组Material 风格的概念设计
项目地址: https://github.com/frogermcs/InstaMaterial
13. SpringIndicator
使用bezier实现粘连效果的页面指示
项目地址: https://github.com/chenupt/SpringIndicator
14. BezierDemo
仿qq消息气泡拖拽 消失的效果。
项目地址: https://github.com/chenupt/BezierDemo
15. FoldableLayout
折叠的信纸被打开一样的动画效果
项目地址: https://github.com/alexvasilkov/FoldableLayout
16.Taurus
下拉刷新,Yalantis 出品。(是不是有点似曾相识呢?)
项目地址: https://github.com/Yalantis/Taurus
17. PersistentSearch
在点击搜索的时候控件在原有位置显示输入框。
项目地址: https://github.com/Quinny898/PersistentSearch
18. circular-progress-button
带进度显示的Button
项目地址: https://github.com/dmytrodanylyk/circular-progress-button
19. discrollview
当上下滚动的时候子元素会呈现不同动画效果的scrollView,网页上称之为:视差滚动
项目地址: https://github.com/flavienlaurent/discrollview
20. sweet-alert-dialog
一个带动画效果的 自定义对话框样式
项目地址: https://github.com/pedant/sweet-alert-dialog
21. android-floating-action-button
Material Desig风格的 浮动操作按钮
项目地址: https://github.com/futuresimple/android-floating-action-button
22. android-collapse-calendar-view
可以在月视图与周视图之间切换的calendar控件
项目地址: https://github.com/blazsolar/android-collapse-calendar-view
22. android-collapse-calendar-view
可以在月视图与周视图之间切换的calendar控件
项目地址: https://github.com/blazsolar/android-collapse-calendar-view
23. NumberProgressBar
个简约性感的数字进度条
项目地址: https://github.com/daimajia/NumberProgressBar
24. CircularProgressView
CircularProgressView 是通过自定义view的方式实现的Material风格的加载提示控件,兼容任何版本。
项目地址: https://github.com/rahatarmanahmed/CircularProgressView
25. OriSim3D-Android
opengl 实现了各种折纸效果,模拟了从一张纸折叠成一条船的整个过程
项目地址: https://github.com/RemiKoutcherawy/OriSim3D-Android
26、万能日历控件:CalendarView
GitHub: https://github.com/huanghaibin-dev/CalendarView
中文使用文档: https://github.com/huanghaibin-dev/CalendarView/blob/master/QUESTION_ZH.md
27、大图查看器: BigImage ImageView ViewPager
Github: https://github.com/SherlockGougou/BigImageViewPager
地址: https://www.jianshu.com/p/b15e65791c3f
支持超长图、超大图的图片浏览器,优化内存,支持手势放大、下拉关闭、查看原图、加载百分比、保存图片等功能。现已支持androidx。
28、安卓工具包androidUntilCode(安卓必备)
Github: https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/README-CN.md
29、万能适配器-BRAVH
官网: http://www.recyclerview.org
GitHub: https://github.com/CymChad/BaseRecyclerViewAdapterHelper
RecyclerView
作为Android最常用的控件之一,是否常常为“她”操碎了心
BRVAH受益群体是所有Android开发者,希望更多开发者能够一起来把这个项目做得更好帮助更多人
30、智能刷新控件--SmartRefreshLayout
GitHub: https://github.com/scwang90/SmartRefreshLayout
中文: https://gitee.com/scwang90/SmartRefreshLayout
SmartRefreshLayout以打造一个强大,稳定,成熟的下拉刷新框架为目标,并集成各种的炫酷、多样、实用、美观的Header和Footer。 正如名字所说,SmartRefreshLayout是一个“聪明”或者“智能”的下拉刷新布局,由于它的“智能”,它不只是支持所有的View,还支持多层嵌套的视图结构。 它继承自ViewGroup 而不是FrameLayout或LinearLayout,提高了性能。 也吸取了现在流行的各种刷新布局的优点,包括谷歌官方的 SwipeRefreshLayout , 其他第三方的 Ultra-Pull-To-Refresh 、 TwinklingRefreshLayout 。 还集成了各种炫酷的 Header 和 Footer。
31、内存泄漏检测工具--leakcanary
使用方式: https://www.jianshu.com/p/b83ddffcb3b5
LeakCanary是Square公司基于MAT开源的一个工具,用来检测Android App中的内存泄露问题。官方地址: https://github.com/square/leakcanary
32、 1218683832 / AndroidSlidingUpPanel
SlidingUpPanelLayout:可以上下滑动的菜单布
https://github.com/1218683832/AndroidSlidingUpPanel
F. Android开发,按钮水波纹
Material Design是Google推出的一个全新的设计语言,它的特点就是拟物扁平化。
Material Design包含了很多内容,大致把它分为四部分:
主题和布局——Android L——Material Design详解(主题和布局)
视图和阴影——ANDROID L——Material Design详解(视图和阴影)
UI控件——ANDROID L——Material Design详解(UI控件)
动画——ANDROID L——Material Design详解(动画篇)
G. 如何在Android中实现渐显按钮的左右滑动效果
左右滑动效果,在android4.0版本以下,需要用代码去控制
android 4.0以上增加了 SwitchButton这样的滑动控件.
自己实现的方式比较多,可以继承Button checkBox方式等等
以下为实现左右滑动效果的控件: