當前位置:首頁 » 安卓系統 » androidstyle

androidstyle

發布時間: 2023-07-25 09:28:37

⑴ Android中style和theme的區別

Theme 和Style資源,將一些通用的屬性提取出來,方便使用。

相同:
Theme和Style位於values文件夾下styles.xml下,格式相同。

[java] view plain
<style name="MyTransparent">
<item name="android:windowBackground">@color/transparent_background</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
<item name="Android:windowNoTitle">?android:windowIsTranslucent</item>其中 問號表示引用本主題中已經定義過的資源的值。

在需要使用的時候調用

[java] view plain
<EditText
android:id="@+id/nameEdit"
<span style="color:#ff0000;"> style="@style/LoginOrRegisterEditStyle"</span>
android:hint="@string/input_real_name"
android:layout_below="@id/tipHead"
android:layout_marginTop="20dp" />

不同:
Theme在AndroidManifest.xml中德Application節點或者Activity節點設置android:theme,或者在對應Activity中通過代碼設置setTheme(),影響整個應用或者一個Activity.
Style一般是在布局中的View中設置。影響單個View,如EditText,TextView等等。

如果主題和樣式屬性有沖突,樣式的屬性高於主題。

如果需要使用系統自帶的主題

[java] view plain
android:theme=<span style="color:#ff0000;">"@android:style</span>/Theme.Light.NoTitleBar"

⑵ android 可以在程序代碼中設置style嗎

Style是View中一些屬性的集合,包括height,padding,fontcolor,background等等,Style單獨定義在xml文件中,類似與web頁面中css的角色,將設計和內容分開,便於修改和重復使用。

1 定義Style

style文件需要保存在res/values目錄下,文件名任意,但是必須是xml文件,sytle文件的根標記必須是<resources>。寫了一個簡單示例,效果如下:

請參考android學習手冊,例子、源碼、文檔全部搞定,採用androidstudo的目錄結構,360手機助手中下載。下面是截圖。

main.xml文件代碼:

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

style="@style/CodeFont"

android:text="測試style">

</TextView>

</LinearLayout>

聲明style是CodeFont,對應的是style文件中的style name。mystyle.xml文件中定義了style name是CodeFont:

parent屬性表示style之間可以繼承,同時可以覆蓋parent style的一些屬性。

<?xmlversion="1.0" encoding="utf-8"?>

<resources>

<style name="CodeFont"parent="@android:style/TextAppearance.Medium">

<itemname="android:layout_width">fill_parent</item>

<itemname="android:layout_height">wrap_content</item>

<itemname="android:textColor">#00FF00</item>

<itemname="android:typeface">monospace</item>

</style>

</resources>

2 Style的繼承

style繼承有兩種方式:

style的繼承可以通過parent屬性,用來繼承android已經定義好的style,例如:

<style name="GreenText" parent="@android:style/TextAppearance">

<itemname="android:textColor">#00FF00</item>

</style>

繼承了android中的TextAppearance,同時覆蓋了android:textColor屬性。

如果要繼承自定義的style,不需要通過parent屬性,只要style的name以需要繼承的style的name開始後跟新的style的name,中間用「.」隔開。注意:這種方式只適用與自定義的style繼承。

<style name="CodeFont.Red">

<itemname="android:textColor">#FF0000</item>

</style>

新的style繼承了CodeFont,則在修改上邊例子中的main.xml為:

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

style="@style/CodeFont.Red"

android:text="測試style">

</TextView>

</LinearLayout>


style也可以多級繼承:

<stylename="CodeFont.Red.Big">

<itemname="android:textSize">30sp</item>

</style> sytle的更多屬性見android包下的R.attr。需要注意,並不是所有的View都支持定義的style的屬性,如果自定義的sytle中包含View不支持的屬性,程序會自動忽略它。

⑶ android 怎麼動態更改view 的style

Android 是可以使用 style的,具體方法為:
1、在Android中可以這樣定義樣式:
在res/values/styles.xml文件中添加以下內容
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=「itcast」>
<item name="android:textSize">18px</item>
<item name="android:textColor">#0000CC</item>
</style>
<纖高/resources>
2、在layout文件中可以像下面這樣使用上面的android樣式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ....>
<TextView style="@style/itcast"
..... />
3、團豎帶可以使他繼承父樣式,當然,如果父樣式的值不符合需求,你也可以對它進行修改,如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="塌蘆itcast">
<item name="android:textSize">18px</item>
<item name="android:textColor">#0000CC</item>
</style>
<style name="subitcast" parent="@style/itcast">
<item name="android:textColor">#FF0000</item>
</style>
</resources>

⑷ android中怎麼創建style

自定義樣式方法,可以直接通過定義xml文件來實現不同的樣式:
只需要修改button_style文件,同樣三種狀態分開定義:
Xml代碼
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient android:startColor="#0d76e1" android:endColor="#0d76e1"
android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>

<item android:state_focused="true">
<shape>
<gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7"
android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="州源伏2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>

<item>
<shape>
<gradient android:startColor="#000000" android:endColor="#ffffff"
android:angle="180" />
<stroke android:width="1dip" android:color="#f403c9"冊攜 />
<corners android:radius="5dip" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</裂做shape>
</item>
</selector>

gradient 主體漸變 startColor開始顏色,endColor結束顏色 ,angle開始漸變的角度(值只能為90的倍數,0時為左到右漸變,90時為下到上漸變,依次逆時針類推)
stroke 邊框 width 邊框寬度,color 邊框顏色
corners 圓角 radius 半徑,0為直角
padding text值的相對位置

⑸ 如何在android style文件中使用自定義屬性

在android style文件中使用自定義屬性是為了方便,只需要這里寫一次就可以在布局文件中多次調用,使用方法如下圖:

1、首先使用android studio打開一個項目,如下圖:

熱點內容
死鎖避免的演算法 發布:2025-02-05 04:43:07 瀏覽:579
python查文檔 發布:2025-02-05 04:27:49 瀏覽:496
javaxmldom 發布:2025-02-05 04:27:40 瀏覽:9
linux修改內存大小 發布:2025-02-05 04:26:05 瀏覽:997
ftp命令復制文件 發布:2025-02-05 04:26:00 瀏覽:303
python好用的ide 發布:2025-02-05 04:14:18 瀏覽:516
id密碼開頭是多少 發布:2025-02-05 04:11:51 瀏覽:101
數據結構c語言ppt 發布:2025-02-05 04:11:45 瀏覽:43
如何用學習機配置的筆寫字 發布:2025-02-05 04:09:15 瀏覽:395
5歲編程 發布:2025-02-05 04:06:21 瀏覽:653