今天记录一下样式和主题和语言国家化和帧动画的实现,今天中午吃的大碗面,加了个卤蛋。好吃的卤蛋总是下一颗呀。在在平常写代码时,经常会遇到重复的时候,这个时候为了偷懒和减少代码的冗余,我们经常对那些重复的地方进行封装。那么今天的主题就是封装style,theme
1Style.
在res-values目录下存在着style.xml文件
当我们写其他的布局文件时,就可以调用这里面封装好的格式样式,用几个简单的textview来演示一下。
首先将activity_main的布局文件写好
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
style="@style/my_text_style"
android:text="textview" />
<TextView
style="@style/my_text_style2"
android:text="textview" />
<TextView
style="@style/my_text_style.my_text_style3"
android:text="textview" />
<TextView
style="@style/my_text_style.night_mode"
android:text="textview" />
</LinearLayout>
style_xml的代码也写好
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="my_text_style">
<item name="android:textSize">20sp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00ff00</item>
</style>
<style name="my_text_style2" parent="my_text_style">
<item name="android:textColor">#0000ff</item>
</style>
<style name="my_text_style.my_text_style3">
<item name="android:textSize">40sp</item>
</style>
<style name="my_text_style.night_mode">
<item name="android:background">#000000</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">50sp</item>
</style>
</resources>
然后我们对代码进行分析,首先activity_main的就不多解释了,就是四个textview
style.xml里面分别写了2种继承写法,分别是
<style name="my_text_style2" parent="my_text_style">
<item name="android:textColor">#0000ff</item>
</style>
<style name="my_text_style.my_text_style3">
<item name="android:textSize">40sp</item>
</style>
--------分割线-------------------------------------------------------------------------------------------------2theme.
theme和style类似,可以自己去设定,不过设定好后需要在manifest中去注册,将相应的主题换成你自己的,不然就会使用默认的。
--------分割线-------------------------------------------------------------------------------------------------
3语言国际化.
这个暂时还用不到,先不写了
--------分割线-------------------------------------------------------------------------------------------------
4帧动画.
这个还是有点意思的,写一下吧
首先要准备好图片资源,最好是gif格式的,其他格式我暂时不知道可不可以,因为没试过,接下来我会去试试。将图片放入drawable目录下,并新建一个xml文件
名字你随意命名,然后在这个xml文件里写代码
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/girl_1"
android:duration="100"/>
<item android:drawable="@drawable/girl_2"
android:duration="100"/>
<item android:drawable="@drawable/girl_3"
android:duration="100"/>
<item android:drawable="@drawable/girl_4"
android:duration="100"/>
<item android:drawable="@drawable/girl_5"
android:duration="100"/>
<item android:drawable="@drawable/girl_6"
android:duration="100"/>
<item android:drawable="@drawable/girl_7"
android:duration="100"/>
<item android:drawable="@drawable/girl_8"
android:duration="100"/>
<item android:drawable="@drawable/girl_9"
android:duration="100"/>
<item android:drawable="@drawable/girl_10"
android:duration="100"/>
<item android:drawable="@drawable/girl_11"
android:duration="100"/>
</animation-list>
duaration是间隔,你可以自己调。
在activity_main里面加一个imageview的控件,在mainactivity里找到这个空间,并将其start。
package com.glsite.frameanim;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = findViewById(R.id.iv);
iv.setBackgroundResource(R.drawable.girl_anim);
AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
anim.start();
}
}
好了,这就是今天的内容了,记录完毕。嘻嘻