zl程序教程

您现在的位置是:首页 >  后端

当前栏目

修改AndroidApp样式风格的方法

方法 修改 样式 风格 AndroidApp
2023-06-13 09:15:11 时间
android中可以自定义主题和风格。风格,也就是style,我们可以将一些统一的属性拿出来,比方说,长,宽,字体大小,字体颜色等等。可以在res/values目录下新建一个styles.xml的文件,在这个文件里面有resource根节点,在根节点里面添加item项,item项的名字就是属性的名字,item项的值就是属性的值,如下所示:
复制代码代码如下:

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

style中有一个父类属性parent,这个属性是说明当前的这个style是继承自那个style的,当然这个style的属性值中都包含那个属性中的,你也可以修改继承到的属性的值,好了,style完成了,我们可以测试一下效果了,先写一个布局文件,比如说一个TextView什么的,可以用到这个style的。这里我就写一个EditText吧。下面是布局文件:
复制代码代码如下:

<?xmlversion="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是应用在Application或者Activity里面的,而Style是应用在某一个View里面的,还是有区别的,好了,废话不多说,还是看代码吧。下面的是style文件:
复制代码代码如下:
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
 <stylename="MyText"parent="@android:style/TextAppearance">
  <itemname="android:textColor">#987456</item>
  <itemname="android:textSize">24sp</item>
 </style>
 <styleparent="@android:style/Theme"name="CustomTheme">
  <itemname="android:windowNoTitle">true</item>
  <itemname="android:windowFrame">@drawable/icon</item>
  <itemname="android:windowBackground">?android:windowFrame</item>
</style>
</resources>

style中有一个父类属性parent,这个属性是说明当前的这个style是继承自那个style的,当然这个style的属性值中都包含那个属性中的,你也可以修改继承到的属性的值,好了,style完成了,我们可以测试一下效果了,先写一个布局文件,比如说一个TextView什么的,可以用到这个style的。这里我就写一个EditText吧。下面是布局文件:
复制代码代码如下:
<?xmlversion="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是应用在Application或者Activity里面的,而Style是应用在某一个View里面的,还是有区别的,好了,废话不多说,还是看代码吧。下面的是style文件:
复制代码代码如下:
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
 <stylename="MyText"parent="@android:style/TextAppearance">
  <itemname="android:textColor">#987456</item>
  <itemname="android:textSize">24sp</item>
 </style>
 <styleparent="@android:style/Theme"name="CustomTheme">
  <itemname="android:windowNoTitle">true</item>
  <itemname="android:windowFrame">@drawable/icon</item>
  <itemname="android:windowBackground">?android:windowFrame</item>
</style>
</resources>

可以看到这里写了一个继承自系统默认的Theme的主题,里面有3个属性,这里强调一下第三个属性的值的问题,这里打个问号,然后加前面的一个item的名字表示引用的是那个名字的值,也就是那个名字对应的图片。
然后我们在Manifest.xml里面的Application里面加一个Theme的属性,这个属性对应的就是我们上面写的Theme。
复制代码代码如下:
<applicationandroid:icon="@drawable/icon"android:label="@string/app_name"
 android:theme="@style/CustomTheme">
<activityandroid:name=".TestStyle"
 android:label="@string/app_name">
<intent-filter>
 <actionandroid:name="android.intent.action.MAIN"/>
 <categoryandroid:name="android.intent.category.LAUNCHER"/>
 </intent-filter>
</activity>

上面的代码没有标题栏,背景和fram都是我们设置的图片。当然也可以在代码中设置主题:
复制代码代码如下:
packagecom.test.shang;
importandroid.app.Activity;
importandroid.os.Bundle;
publicclassTestStyleextendsActivity{
 @Override
 protectedvoidonCreate(BundlesavedInstanceState){
  super.onCreate(savedInstanceState);
  setTheme(R.style.CustomTheme);
  setContentView(R.layout.test_style);
 }
}