android設置style
① android 可以在程序代碼中設置style嗎
當然可以嘍,可以在onCreate的時候就設置,setStyle()
② android 可以在程序代碼中設置style嗎
如果你是給textView的話鎮宏,可以
textView.setTextAppearance(this, R.style.MyStyle);
其它的耐圓話就不行了昌旅塌。
③ android 可以在程序代碼中設置style嗎
<style name="text_style">
<item name="android:textStyle">bold</item>
<item name="android:textSize">18sp</item>
</style>
xml使用:
<TextView
style="@style/button_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TextView>
java代碼使用:
TextView txtname = new TextView(this);
txtname.setTextAppearance(this, R.style.text_style);
④ 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嗎
可明侍以的,步驟如下:
在項目的values/styles.xml文件里定義style屬性
⑥ android 可以在程序代碼中設置style嗎
可以的,步驟如下:
1、在項目的歲悔values/styles.xml文件里定義鋒雀困style屬性。
⑦ 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文件中使用自定義屬性
在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>
⑩ android 可以在程序代碼中設置style嗎
可以,當然可以嘍,可以在onCreate的時候就設置,setStyle() 無論換背景、顏色、字體什麼的 首先要得到被換的component, 比如字體,假設他是TextView: 1、得到這個TextView component:TextView tv = (TextView)findViewById(R.id.tv);