Android样式style
android app汉化与英化
在res文件夹下面添加一个values-en-US文件夹,添加一个strings.xml文件,然后往里面添加标签对,系统语言换成英语就可以实现英化了。
汉化
英化
theme统一样式风格
theme意思是主题,在这里面设置app的主题布局
点进去之后进入了style.xml页面,发现默认风格是继承父类,删除了之后就恢复系统默认色彩
就是在style.xml里面修改系统风格
添加一段标签代码,修改基本主题布局,以及修改默认按钮样式
<style name="AppTheme" >
<item name="android:windowNoTitle">true</item>
<item name="android:background">#aa000a</item>
<item name="android:textSize">30dp</item>
<item name="android:textColor">#000000</item>
</style>
<style name="ButtonStyle" >
<item name="android:textSize">20dp</item>
<item name="android:textColor">#0f0</item>
<item name="android:layout_weight">1</item>
</style>
创建一个drawable文件夹,存放一个专门设置按钮的页面,XML页面类型选择shape,这里可以设置页面样式,我写了按钮圆角,按钮大小,在values文件夹创建一个存放颜色的XML文件,然后再这个shape文件引用。
最后一定要为每一个按钮添加一个主题引用
<Button
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="onclick_but1"
android:id="@+id/but1"
android:text="but1"
android:background="@drawable/btn_shap"
/>
最后效果是这样
Selector选择器
用selector可以用来更换背景图片
在drawble文件夹再创建一个btn_shap_press.xml文件,然后在主题xml文件里面更改按钮样式中的背景图片换成选择器,在选择器中关联触发事件的shap.xml文件就可以了。
selector内容
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/btn_shap_press"></item>
<item android:state_pressed="false" android:drawable="@drawable/btn_shap"></item>
</selector>
动画
旋转、平移、缩放、渐变
public void rotate(View v) {// 旋转
float fromDegrees = 0;
float toDegrees = 360;
int pivotXType = Animation.RELATIVE_TO_SELF;
float pivotXValue = 0.5f;
int pivotYType = Animation.RELATIVE_TO_SELF;
float pivotYValue = 0.5f;
RotateAnimation anim = new RotateAnimation(fromDegrees, toDegrees,
pivotXType, pivotXValue, pivotYType, pivotYValue);
anim.setRepeatCount(1);
anim.setDuration(2000);// ms
ivlogo.startAnimation(anim);
}
public void translate(View v) {// 平移
int fromXType = Animation.RELATIVE_TO_SELF;
float fromXValue = 0;
int toXType = Animation.RELATIVE_TO_SELF;
float toXValue = 0;
//
int fromYType = Animation.RELATIVE_TO_SELF;
float fromYValue = 0;
int toYType = Animation.RELATIVE_TO_SELF;
float toYValue = 1;
TranslateAnimation anim = new TranslateAnimation(fromXType, fromXValue,
toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
anim.setRepeatCount(1);
anim.setDuration(2000);// ms
ivlogo.startAnimation(anim);
}
public void scale(View v) {// 大小缩放
float fromX = 1;
float toX = 2;
int pivotXType = Animation.RELATIVE_TO_SELF;
float pivotXValue = 0.5f;
float fromY = 1;
float toY = 2;
int pivotYType = Animation.RELATIVE_TO_SELF;
float pivotYValue = 0.5f;
ScaleAnimation anim = new ScaleAnimation(fromX, toX, fromY, toY,
pivotXType, pivotXValue, pivotYType, pivotYValue);
anim.setRepeatCount(1);
anim.setDuration(2000);// ms
ivlogo.startAnimation(anim);
}
public void alpha(View v) {//渐变
float fromAlpha = 1.0f;
float toAlpha = 0.0f;
AlphaAnimation anim = new AlphaAnimation(fromAlpha, toAlpha);
anim.setRepeatCount(1);
anim.setDuration(2000);// ms
ivlogo.startAnimation(anim);
}
也可以用xml文件来制作动画,也可以组合几种动画
在res文件夹下面创建一个anim文件夹,创建一个alpha.xml文件和translate.xml文件,然后在监听方法里面关联xml文件,就可以播放动画了。
这里面是XML文件的代码
public void set_5(View v) {
AnimationSet animationSet = new AnimationSet(true);
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
Animation anim2 = AnimationUtils.loadAnimation(this, R.anim.translate);
animationSet.addAnimation(anim);
animationSet.addAnimation(anim2);
ivlogo.startAnimation(animationSet);
}