android自定义样式
A. Android 中怎么设置全局自定义字体样式
使用stackoverflow软件进行修改。
操作
首先下载自定义字体,拷贝到工程中的assets文件夹下,建个新文件夹也可以。
创建一个继承自Application的类,放上TypeFace的变量。
将系统的serif的字体替换成微软雅黑。
最后自定义的主题。
B. 如何修改安卓自定义水平progressdialog的样式
android修改HOLO对话框风格
andriod中修改对话框的风格,可以通过设置theme来实现,部分元素需要通过Java代码来修改,下面以修改对话框的标题为例说明各步骤。
1、编写一个文本样式。
DIALOG的标题是一个textview,在sytles.xml中,添加如下代码来设置你自己的文本样式:
<style name="DialogWindowTitle">
<item name="android:textSize">22sp</item>
<item name="android:textColor">@color/font_dark_grey</item>
</style>
2、设置对话框的标题主题。
上面的标题文本并不能直接设置为对话框的标题样式。 我们还需要编写一个表示标题的主题的style,在这里指定标题的文本样式。代码如下:
<style name="DialogWindowTitle.DeviceDefault">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">@style/DialogWindowTitle</item>
</style>
3、设置对话框主题。
接下来,我们编写我们的对话框主题,在这里指定标题的主题。由于一些属性并不是public的,所以我们需要继承自原来的某个style,代码如下:
<!--Dialog主题-->
<style name="Theme.DeviceDefault.Dialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog" >
<item name="android:windowTitleStyle">@style/DialogWindowTitle.DeviceDefault</item>
</style>
4、自定义App的主题。
接下来,我们需要在我们的App theme中指定我们的对话框使用这种主题,所以需要定义一个App theme。同样由于App theme的许多属性并不是public的(比如下面要提到的标题下面的那条蓝线),所以我们要继承自一个原生的style。这里我根据程序需要选择了Theme.Holo.Light.NoActionBar,代码如下:
<style name="ParkingTheme" parent="@android:style/Theme.Holo.Light.NoActionBar">
<item name="android:dialogTheme">@style/Theme.DeviceDefault.Dialog</item>
</style>
5、指定App主题。
最后一步,我们需要在AndroidManifest.xml文件中,指定我们的app主题。这步很简单,只需要在application标签中指定android:theme的值即可,如下:
android:theme="@style/ParkingTheme"
C. 如何为Android studio设置自定义的主题样式
Android Studio默认主题IntelliJ,我们可以修改成黑色的Dracula的主题或者是Windows主题。 1、首先双击桌面AndroidStudio图标,打开Android Studio。 2、选择Android Studio菜单栏File——Settings选项 3、或者在工具栏中直接点击Settings设置图。详细的可以看看安卓巴士教程:http://www.apkbus.com/thread-463460-1-1.html
D. 如何自定义android Button样式
1)自定义button样式
一、采用图片方式
首先新建Android XML文件,类型选Drawable,根结点选selector,自定义一个文件名。
随后,开发环境自动在新建的文件里加了selector结点,我们只需要在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/play_press" ;/>
<item android:state_focused="true" android:drawable="@drawable/play_press" ;/>
<item android:drawable="@drawable/play" ;/>
</selector>
注:这里获取焦点跟点击时显示的是同一张图片,必须严格照上面的顺序写,不可倒。
最后,只要在布局时写Button控件时应用到Button的Background属性即可,如:
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_style">
</Button>
二、采用自定义方式
在源代码中,只需要修改button_style文件,同样三种状态分开定义:
<?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值的相对位置
2)自定义style样式
一、在style.xml中自定义样式
以自定义text文本大小和颜色为例,自定义一个名称为"testStyle"的style代码如下:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
<style name="testStyle">
<item name="android:textSize">30px</item>
<item name="android:textColor">#1110CC</item>
<item name="android:width">150dip</item>
<item name="android:height">150dip</item>
</style>
</resources>
二、在layout文件中引用自定义的"testStyle"的style样式
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
style="@style/testStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
E. 如何自定义android Button样式
在windows7操作系统Android studio中按照如下方法定义button的样式。
1、首先使用Android studio创建一个项目,项目结构如下:
F. android 自定义view 怎么规定view的样式
android 自定义view的样式的实现:
1.在values文件夹下,打开attrs.xml,其实这个文件名称可以是任意的,写在这里更规范一点,表示里面放的全是view的属性。
2.因为我们下面的实例会用到2个长度,一个颜色值的属性,所以我们这里先创建3个属性。
<declare-styleable name="rainbowbar">
<attr name="rainbowbar_hspace" format="dimension"></attr>
<attr name="rainbowbar_vspace" format="dimension"></attr>
<attr name="rainbowbar_color" format="color"></attr>
</declare-styleable>
举例说明:
蓝色的进度条
public class RainbowBar extends View {
//progress bar color
int barColor = Color.parseColor("#1E88E5");
//every bar segment width
int hSpace = Utils.dpToPx(80, getResources());
//every bar segment height
int vSpace = Utils.dpToPx(4, getResources());
//space among bars
int space = Utils.dpToPx(10, getResources());
float startX = 0;
float delta = 10f;
Paint mPaint;
public RainbowBar(Context context) {
super(context);
}
public RainbowBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RainbowBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//read custom attrs
TypedArray t = context.obtainStyledAttributes(attrs,
R.styleable.rainbowbar, 0, 0);
hSpace = t.getDimensionPixelSize(R.styleable.rainbowbar_rainbowbar_hspace, hSpace);
vSpace = t.getDimensionPixelOffset(R.styleable.rainbowbar_rainbowbar_vspace, vSpace);
barColor = t.getColor(R.styleable.rainbowbar_rainbowbar_color, barColor);
t.recycle(); // we should always recycle after used
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(barColor);
mPaint.setStrokeWidth(vSpace);
}
.......
}
View有了三个构造方法需要我们重写,这里介绍下三个方法会被调用的场景,
第一个方法,一般我们这样使用时会被调用,View view = new View(context);
第二个方法,当我们在xml布局文件中使用View时,会在inflate布局时被调用,
<View layout_width="match_parent" layout_height="match_parent"/>。
第三个方法,跟第二种类似,但是增加style属性设置,这时inflater布局时会调用第三个构造方法。
<View style="@styles/MyCustomStyle" layout_width="match_parent" layout_height="match_parent"/>。
G. Android自定义Switch样式
如图自定义的Switch样式:
需要注意的是:switch的大小是跟thumb的大小有关
下面以样式二为例:
switch_track_bg_selected.xml
switch_track_bg_normal.xml
switch_thumb_bg_selected.xml
switch_thumb_bg_normal.xml
详细参考: Android Switch自定义样式