国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

修改Android App樣式風(fēng)格的方法

2020-04-11 12:01:04
字體:
供稿:網(wǎng)友
android中可以自定義主題和風(fēng)格。風(fēng)格,也就是style,我們可以將一些統(tǒng)一的屬性拿出來,比方說,長,寬,字體大小,字體顏色等等。可以在res/values目錄下新建一個styles.xml的文件,在這個文件里面有resource根節(jié)點(diǎn),在根節(jié)點(diǎn)里面添加item項(xiàng),item項(xiàng)的名字就是屬性的名字,item項(xiàng)的值就是屬性的值,如下所示:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="MyText" parent="@android:style/TextAppearance">
     <item name="android:textColor">#987456</item>
  <item name="android:textSize">24sp</item>
 </style>
</resources>

style中有一個父類屬性parent, 這個屬性是說明當(dāng)前的這個style是繼承自那個style的,當(dāng)然這個style的屬性值中都包含那個屬性中的,你也可以修改繼承到的屬性的值,好了,style完成了,我們可以測試一下效果了,先寫一個布局文件,比如說一個TextView什么的,可以用到這個style的。這里我就寫一個EditText吧。下面是布局文件:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
<EditText
 android:id="@+id/myEditText"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 style="@style/MyText"
 android:text="測試一下下"/>
</LinearLayout>

說完了style,下面就說說Theme,Theme跟style差不多,但是Theme是應(yīng)用在Application或者Activity里面的,而Style是應(yīng)用在某一個View里面的,還是有區(qū)別的,好了,廢話不多說,還是看代碼吧。下面的是style文件:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="MyText" parent="@android:style/TextAppearance">
  <item name="android:textColor">#987456</item>
  <item name="android:textSize">24sp</item>
 </style>
 <style parent="@android:style/Theme" name="CustomTheme">
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFrame">@drawable/icon</item>
  <item name="android:windowBackground">?android:windowFrame</item>
</style>
</resources>

style中有一個父類屬性parent, 這個屬性是說明當(dāng)前的這個style是繼承自那個style的,當(dāng)然這個style的屬性值中都包含那個屬性中的,你也可以修改繼承到的屬性的值,好了,style完成了,我們可以測試一下效果了,先寫一個布局文件,比如說一個TextView什么的,可以用到這個style的。這里我就寫一個EditText吧。下面是布局文件:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
<EditText
 android:id="@+id/myEditText"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 style="@style/MyText"
 android:text="測試一下下"/>
</LinearLayout>

說完了style,下面就說說Theme,Theme跟style差不多,但是Theme是應(yīng)用在Application或者Activity里面的,而Style是應(yīng)用在某一個View里面的,還是有區(qū)別的,好了,廢話不多說,還是看代碼吧。下面的是style文件:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="MyText" parent="@android:style/TextAppearance">
  <item name="android:textColor">#987456</item>
  <item name="android:textSize">24sp</item>
 </style>
 <style parent="@android:style/Theme" name="CustomTheme">
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFrame">@drawable/icon</item>
  <item name="android:windowBackground">?android:windowFrame</item>
</style>
</resources>

可以看到這里寫了一個繼承自系統(tǒng)默認(rèn)的Theme的主題,里面有3個屬性,這里強(qiáng)調(diào)一下第三個屬性的值的問題,這里打個問號,然后加前面的一個item的名字表示引用的是那個名字的值,也就是那個名字對應(yīng)的圖片。
然后我們在Manifest.xml里面的Application里面加一個Theme的屬性,這個屬性對應(yīng)的就是我們上面寫的Theme。
復(fù)制代碼 代碼如下:

<application android:icon="@drawable/icon" android:label="@string/app_name"
 android:theme="@style/CustomTheme">
<activity android:name=".TestStyle"
 android:label="@string/app_name">
<intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity>

上面的代碼沒有標(biāo)題欄,背景和fram都是我們設(shè)置的圖片。當(dāng)然也可以在代碼中設(shè)置主題:
復(fù)制代碼 代碼如下:

package com.test.shang;
import android.app.Activity;
import android.os.Bundle;
public class TestStyle extends Activity {
 @Override
 protected void onCreate (Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setTheme(R.style.CustomTheme);
  setContentView(R.layout.test_style);
 }
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长武县| 佳木斯市| 斗六市| 保德县| 林州市| 驻马店市| 米泉市| 凌源市| 辉南县| 崇信县| 厦门市| 铜川市| 大石桥市| 汤原县| 锦屏县| 建昌县| 韩城市| 东兰县| 轮台县| 襄城县| 新昌县| 玉屏| 巧家县| 西峡县| 武川县| 靖安县| 临夏县| 中方县| 金昌市| 垣曲县| 济源市| 运城市| 尚志市| 珲春市| 平江县| 揭西县| 治县。| 阳泉市| 延津县| 郸城县| 广东省|