當前位置:首頁 » 安卓系統 » android漂亮的按鈕

android漂亮的按鈕

發布時間: 2022-12-18 21:46:26

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方式等等

以下為實現左右滑動效果的控制項:

熱點內容
qq怎樣清理緩存 發布:2025-04-01 03:08:06 瀏覽:388
python定義成員變數 發布:2025-04-01 02:51:37 瀏覽:21
平板怎麼解除密碼 發布:2025-04-01 02:47:55 瀏覽:78
在配置命令的時候輸錯了怎麼刪除 發布:2025-04-01 02:42:01 瀏覽:688
正當訪問案例 發布:2025-04-01 02:32:27 瀏覽:971
多媒體存儲設備有哪些 發布:2025-04-01 02:31:35 瀏覽:124
華鑫證券配置怎麼樣 發布:2025-04-01 02:31:31 瀏覽:813
java代碼混淆工具 發布:2025-04-01 02:24:02 瀏覽:91
蘋果電腦鏈接伺服器 發布:2025-04-01 02:22:27 瀏覽:865
游戲雲伺服器好不好 發布:2025-04-01 02:21:47 瀏覽:735