当前位置:首页 » 安卓系统 » android自定义button样式

android自定义button样式

发布时间: 2022-07-06 04:25:14

① android 中如何自定义dialog里button的背景颜色

选定Alterdialog所关联的layout文件即可
就像一般的页面是
setLayoutXXX什么的
忘记了

② android 怎么动态设置button 的style

自定义样式方法,可以直接通过定义xml文件来实现不同的样式:
只需要修改button_style文件,同样三种状态分开定义:
Xml代码
<?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值的相对位置

③ 如何自定义RadioButton样式

我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式。
最近用到RadioButton,利用xml修改android:background="@drawable/button_drawable",其中button_drawable为自己定义的.xml文件(res/drawable文件下),但是不成功,到网上查找,也没有正确的说法,我就开始自己尝试,最后做好了。
其实方法很简单,同样在res/drawable新建radiobutton.xml如下

01 <selector xmlns:android="http://schemas.android.com/apk/res/android">
02 <item
03 android:state_enabled="true"
04 android:state_checked="true"
05 android:drawable="@drawable/check" />
06 <item
07 android:state_enabled="true"
08 android:state_checked="false"
09 android:drawable="@drawable/checknull" />
10 </selector>

check和checknull分别为选中和位选中的图片。

然后在你的布局文件中,RadioButton 布局

设置android:button = "@drawable/radiobutton",就可以了!

④ android中button上设置图片

android中button上设置图片的方法为:
1、自定义MyButton类
public class MyButton extends Button {
//This constructormust be
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context) {
super(context);
}
private Paint mPaint = null;
private String mText;
private int mX, mY;
public void onSetText(String text, int nLeft, int nBottom, int nTextSize,
int nTextColor) {
mPaint = new Paint();
mPaint.setTextSize(nTextSize);
mPaint.setColor(nTextColor);
this.mText = text;
this.mX = nLeft;
this.mY = nBottom;
}
private int mDownBmpId, mUpBmpId;
public void onSetBmp(int nDownID, int nUpID) {
this.mDownBmpId = nDownID;
this.mUpBmpId = nUpID;
}
@Override
public void onDraw(Canvas canvas) {
if (mPaint != null)
canvas.drawText(mText, mX, mY, mPaint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
super.setBackgroundResource(mDownBmpId);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
super.setBackgroundResource(mUpBmpId);
}
return super.onTouchEvent(event);
}
}

2、 在xml布局文件中添加MyButton控件,像应用普通的Button控件一样。
<com.MyButton
android:id="@+id/test_btn" android:layout_width="120px"
android:layout_height="fill_parent" android:text="Test"
android:background="@drawable/btn_u" />
其中com.MyButton是你定义的MyButton类所在的包名
3、在onCreate()中加载MyButton控件。
MyButton btn = (MyButton)findViewById(R.id.test_btn);
btn.onSetBmp(R.drawable.btn_d, R.drawable.btn_u);
其中btn_d表示为按下btn时背景图片,btn_u为默认状态下btn背景图片。

⑤ android怎么改变button中字体的格式

使用方法 第一种是在listview中配置android:listSelector=”@drawable/list_item_bg” 第二种是在listview的item中添加属性android:background=”@drawable/list_item_bg” 第三种是java代码中使用: Drawable drawable = getResources()/apk/res/android">" target="_blank">schemas/apk/res/android"></a> <item android:state_selected="true" android:color="#FF0000" /> <item android:state_focused="true" android:color="#00FF00" /> <item android:state_pressed="true" android:color="#0000FF" /> <item android:color="#000000" /></selector>使用方法 android:textColor="@drawable/button_color" 更复杂的效果 还可以实现更复杂的效果,例如渐变等等。 drawable/button_color/apk/res/android">" target="_blank">schemas/apk/res/android"></a> <item android:state_pressed="true"> <!-- 定义当button 处于pressed 状态时的形态。--> <shape> <gradient android:startColor="#8600ff" /> <stroke android:width="2dp" android:color="#000000" /> <corners android:radius="5dp" /> <padding android:left="10dp" android:top="10dp" android:bottom="10dp" android:right="10dp"/> </shape> </item> <item android:state_focused="true"> <!-- 定义当button获得 focus时的形态 --> <shape> <gradient android:startColor="#eac100"/> <stroke android:width="2dp" android:color="#333333" color="#ffffff"/> <corners android:radius="8dp" /> <padding android:left="10dp" android:top="10dp" android:bottom="10dp" android:right="10dp"/> </shape> </item></selector>使用方法 android:background="@drawable/button_color" android:focusable="true"

⑥ android button 样式怎么写

如何自定义android Button样式

android自带的样式比较难看,如何能够自定义按钮的样式,使其显示的跟美工设计的效果一样,现与大家分享下

工具/原料

eclipse ADT

方法/步骤

在layout中添加2个按钮,从下图中可以看出在按钮中调用了style和android:background属性,这两个属性一个是自定义样式,一个是给按钮添加背景图片,下面详细介绍下

展开res目录,可以看到在values目录下有styles.xml文件,该文件用于自定义样式,双击打开

下图中标注的是我自定义的样式,name为BtnStyle,当按钮调用自定义样式的时候访问这个name

下图是在button中调用自定义样式的方法,比较简单

下面分享下如何往按钮中添加自定义图片,使按钮看起来更漂亮些,因不同手机分辨率不同,那必然牵扯到图片的拉伸,在android系统下有个很好的技术“九宫格“,可以对图片进行处理,只对局部进行拉伸,给工具目录存储在android\sdk\tools\draw9patch.bat,经过该工具处理的图片以.9.png结尾,放到drawable文件夹

下图是在Button中通过android:background属性加载图片的方法,至此我们自定义的按钮样式也就完成了,当然这只是个引子,在具体的项目工程中实现的效果要比这个demo复杂很多,有好的设计思路欢迎交流。

出自:http://jingyan..com/article/454316ab4bdc66f7a7c03a13.html

⑦ android studio中的button有哪些方法

先介绍下修改原理:首先打开位于android.widget包下面的Button.java文件,这里有一句关键的代码如下:
public
Button(Context
context,
AttributeSet
attrs)
{
this(context,
attrs,
com.android.internal.R.attr.buttonStyle);
}
1
2
3
其中com.android.internal.R.attr.buttonStyle就是我们修改样式的关键了,网上的教程的修改方法大都是:
<Button
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:text="价格"
/>
1
2
3
4
5
6
也就是在对应的xml里面button控件里面编写style达到目的。
但是如果我们的app需要完全统一整个应用的button的样式,那么就需要在每一个button里面添加style。
这显然效率太低下了。
接下来打开我们项目中values文件夹下面的styles.xml文件,我们创建安卓项目的时候,会有一个默认的styles文件。
打开之后找到这段代码:
<style
name="AppBaseTheme"
parent="Theme.Holo.Light">
<!--
Theme
customizations
available
in
newer
API
levels
can
go
in
res/values-vXX/styles.xml,
while
customizations
related
to
backward-compatibility
can
go
here.
-->
</style>
<!--
Application
theme.
-->
<style
name="AppTheme"
parent="AppBaseTheme">
1
2
3
4
5
6
7
8
9
10
不保证读者的默认styles.xml和我的是一样的,不过大概是这个样子,有可能读者的最低支持是2.3、那么就没有Them.Light。
我们使用eclipse的快捷键打开这个Theme.Holo.Light。可以看到如下代码:
<style
name="Theme.Holo.Light"
parent="Theme.Light">
<item
name="colorForeground">@android:color/bright_foreground_holo_light</item>
<item
name="colorForegroundInverse">@android:color/bright_foreground_inverse_holo_light</item>
<item
name="colorBackground">@android:color/background_holo_light</item>
<item
name="colorBackgroundCacheHint">@android:drawable/background_cache_hint_selector_holo_light</item>
<item
name="disabledAlpha">0.5</item>
<item
name="backgroundDimAmount">0.6</item>

⑧ 安卓UI自定义radiobutton样式

自定义RadioButton样式分为两步:

1、自定义好样式:

打开style.xml,添加一个item

<stylename="RadiobuttonStyle">
<itemname="android:gravity">center</item>
<itemname="android:textSize">16sp</item>
<itemname="android:textColor">@color/text_select_color</item>
<itemname="android:button">@null</item>
<itemname="android:drawableTop">@drawable/select_rbtn_home</item>
</style>

2、在RadioButon中引用:

<RadioButton
android:id="@+id/tv_tab_home"
style="@style/RadiobuttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/tab_home"/>

⑨ android开发 设置自定义按钮为半透明

半透明<Button android:background="#e0000000" ... />
透明<Button android:background="#00000000" ... />
颜 色和不透明度 (alpha) 值以十六进制表示法表示。任何一种颜色的值范围都是 0 到 255(00 到 ff)。对于 alpha,00 表示完全透明,ff 表示完全不透明。表达式顺序是“aabbggrr”,其中“aa=alpha”(00 到 ff);“bb=blue”(00 到 ff);“gg=green”(00 到 ff);“rr=red”(00 到 ff)。例如,如果您希望对某叠加层应用不透明度为 50% 的蓝色,则应指定以下值:7fff0000
设置背景图片透明度(超简单)
Java代码
View v = findViewById(R.id.content);//找到你要设透明背景的layout 的id
v.getBackground().setAlpha(100);//0~255透明度值

⑩ 怎么Android编程中设置Button的字体颜色呢

<Button
android:text="123"
android:textColor="@color/aliceblue" //设置字体颜色
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
望采纳!

热点内容
scratch少儿编程课程 发布:2025-04-16 17:11:44 浏览:628
荣耀x10从哪里设置密码 发布:2025-04-16 17:11:43 浏览:357
java从入门到精通视频 发布:2025-04-16 17:11:43 浏览:74
php微信接口教程 发布:2025-04-16 17:07:30 浏览:298
android实现阴影 发布:2025-04-16 16:50:08 浏览:788
粉笔直播课缓存 发布:2025-04-16 16:31:21 浏览:338
机顶盒都有什么配置 发布:2025-04-16 16:24:37 浏览:203
编写手游反编译都需要学习什么 发布:2025-04-16 16:19:36 浏览:801
proteus编译文件位置 发布:2025-04-16 16:18:44 浏览:357
土压缩的本质 发布:2025-04-16 16:13:21 浏览:583