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

androidstyle文件

發布時間: 2022-08-19 06:04:41

Ⅰ android 為什麼在style.xml文件里的其中一個style中的item前面隨便加個符編譯號不報錯

因為html很寬容,安卓支持html,只有你大概語法沒有錯誤,一般都能編譯成功。

Ⅱ 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怎樣寫style

在values文件夾下的styles中添加類似
<style name="Button_style" >
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">18sp</item>
<item name="android:singleLine">true</item>
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center</item>
</style>
中間的item可以自己添加想要的效果
然後在Button中屬性添加style="@style/Button_style"!

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

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

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

Ⅳ 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 TextView怎麼引用Style

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

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

Ⅶ 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>

熱點內容
ios應用上傳 發布:2024-09-08 09:39:41 瀏覽:438
ios儲存密碼哪裡看 發布:2024-09-08 09:30:02 瀏覽:870
opensslcmake編譯 發布:2024-09-08 09:08:48 瀏覽:653
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:744
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:173
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:780
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:101
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:209
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995