全心思齐网

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

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>

匿名回答于2024-05-24 00:33:40


回答如下:Button是Android中的一个View组件,继承自TextView,除了TextView提供的方法外,还有以下方法:

1. setOnClickListener(View.OnClickListener listener):设置点击事件监听器

2. setEnabled(boolean enabled):设置按钮是否可用

3. setTextColor(int color):设置按钮文本颜色

4. setBackgroundColor(int color):设置按钮背景颜色

5. setTextSize(float size):设置按钮文本大小

6. setText(CharSequence text):设置按钮文本内容

7. setPadding(int left, int top, int right, int bottom):设置按钮的内边距

8. setAllCaps(boolean allCaps):设置按钮文本是否全部大写

9. setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom):设置按钮文本左、上、右、下四个方向的图标

10. setCompoundDrawablePadding(int pad):设置按钮文本与图标之间的距离

11. setGravity(int gravity):设置按钮文本的对齐方式

12. setMaxLines(int maxLines):设置按钮文本最大行数

13. setOnClickListener(View.OnClickListener listener):设置按钮点击事件的监听器

14. setOnLongClickListener(View.OnLongClickListener listener):设置按钮长按事件的监听器

15. setOnTouchListener(View.OnTouchListener listener):设置按钮触摸事件的监听器

16. setVisibility(int visibility):设置按钮的可见性

17. setTextAlignment(int textAlignment):设置按钮文本的对齐方式

18. setTypeface(Typeface typeface):设置按钮文本的字体

19. setTransformationMethod(TransformationMethod transformationMethod):设置按钮文本的转换方式

20. setShadowLayer(float radius, float dx, float dy, int color):设置按钮文本的阴影效果

匿名回答于2024-05-17 03:46:56


相关知识问答