button属性android
① 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里的BUTTON里的属性问题
你这看上去很是矛盾啊。。。你把右和底去掉别写。。你最下面那句是距离顶30dip..可是你上面的paddingTop为什么写成0啊。。具我实测。。这个padding 和margin 在某种意义上是一样的效果。。
③ android 中button控件都有些什么常用的属性
<Button
android:id="@+id/loginButton"
android:layout_width="50dp
android:layout_height="@dimen/button_footer_height"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:text="Login"
android:textSize="12dp"
android:visibility="invisible" />
我用到这些
④ android中怎样给button中的字设置颜色
我们可以使用selector来实现Button的特效,如图所示:
默认情况
获得焦点的时候
点击按钮
main.xml
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按下或者获得焦点Button会变不同颜色"
<SPAN style="COLOR: #ff0000">android:textColor="@color/button_text" </SPAN>/>
</LinearLayout>
www.2cto.com
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按下或者获得焦点Button会变不同颜色"
android:textColor="@color/button_text" />
</LinearLayout>
XML 文件保存在res/color/button_text.xml
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true" android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
背景选择器-selector
概述
在drawable/xxx.xml中配置,通过配置selector,可以使系统运行时根据控件对象的状态使用相应的图片、文字等。
selector中的常用属性
android:state_selected 控件选中状态,可以为true或false
android:state_focused 控件获得焦点状态,可以为true或false
android:state_pressed 控件点击状态,可以为true或false
android:state_enabled 控件使能状态,可以为true或false
android:state_checkable 控件可勾选状态,可以为true或false
android:state_checked 控件勾选状态,可以为true或false
注意:在状态描述中,第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,所以默认的值必须总是在最后。
android:window_focused 应用程序窗口焦点状态,可以为true或false
android:color 定义特定状态的颜色
#rgb
#argb
#rrggbb
#aarrggbb
为16进制颜色。这个颜色由rgb值指定,可带alpha,必须以”#“开头,后面跟随alpha-red-green-blue信息,格式可以为:
使用selector设置背景
把下面的XML保存成.xml文件(比如list_item_bg.xml),运行时系统会根据ListView中列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片 -->
<item android:drawable="@drawable/pic1" />
<!-- 没有焦点时的背景图片 -->
<item android:state_window_focused="false"
android:drawable="@drawable/pic1" />
...
⑤ android 如何设置button的大小
在相对应的layout.xml文件中,button下可以定义android:layout_width和android:layout_height两个属性可以用来控制button的大小,希望可以帮助到你!
⑥ 设置 Button 的元素属性 android:text 为 Exit 。
上面说“把Exit改成中文”的回答是正确的。
猜测是汉化时把“Exit”汉化成“退出”了。用原版应该不会有这个问题。
与此类似,后面让添加一个按钮把里面的文字改成“Help”,也得写“帮助”才能过去。
参考:网页链接
⑦ 如何自定义android Button样式
在windows7操作系统Android studio中按照如下方法定义button的样式。
1、首先使用Android studio创建一个项目,项目结构如下:
⑧ android button怎么用
千锋扣丁学堂Android开发为您解答:
Button基本使用方法
首先,添加Button控件到XML布局文件中。也可通过程序添加。
在布局文件中设置按钮的一些属性,如位置,宽高,按钮上的字,颜色等。
比较重要的是要给按钮一个id号,这是按钮唯一的名字。
这样在程序中可以通过如下形式获得按钮:
button = (Button)findViewById(R.id.buttonId);
处理按钮点击
按钮点击有两种处理方法。
第一种是通过onClick属性,通过这个属性设置处理点击事件的方法名,在Activity中实现这个方法。
另一种方法是典型的事件监听机制的应用形式,下面详细说明这两种方法。
1.通过onClick属性设置处理方法
在XML布局文件中设置Button的属性:
android:onClick="yourMethodName"
然后在该布局文件对应的Acitivity中实现该方法:
/** Called when the user touches the button */
public void yourMethodName(View view)
{
// Do something in response to button click
}
需要注意的是这个方法必须符合三个条件:
1.public
2.返回void
3.只有一个参数View,这个View就是被点击的这个控件。
2.使用setOnClickListener添加监听器对象
可以写一个内部类,实现OnClickListener接口,在这个类中实现onClick方法,方法里面写在按钮点击时想做的具体工作。
将这个内部类的对象传入按钮的setOnClickListener方法中,即完成监听器对象和按钮的绑定(在事件源Button上注册了事件监听器),这时候只要按钮被点击,那么监听器对象的onClick方法就会被调用。
当然这里也不一定要自己写一个内部类出来,比如这个例子:
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Do something in response to button click
}
});
⑨ android button 的全部属性 谢谢
看文档啊
继承自 VIEW VIEW 有的属性它都能用
XML Attributes
Attribute Name Related Method Description
android:autoLink setAutoLinkMask(int) Controls whether links such as urls and email addresses are automatically found and converted to clickable links.
android:autoText setKeyListener(KeyListener) If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors.
android:bufferType setText(CharSequence,TextView.BufferType) Determines the minimum type that getText() will return.
android:capitalize setKeyListener(KeyListener) If set, specifies that this TextView has a textual input method and should automatically capitalize what the user types.
android:cursorVisible setCursorVisible(boolean) Makes the cursor visible (the default) or invisible.
android:digits setKeyListener(KeyListener) If set, specifies that this TextView has a numeric input method and that these specific characters are the ones that it will accept.
android:drawableBottom (int,int,int,int) The drawable to be drawn below the text.
android:drawableLeft (int,int,int,int) The drawable to be drawn to the left of the text.
android:drawablePadding setCompoundDrawablePadding(int) The padding between the drawables and the text.
android:drawableRight (int,int,int,int) The drawable to be drawn to the right of the text.
android:drawableTop (int,int,int,int) The drawable to be drawn above the text.
android:editable If set, specifies that this TextView has an input method.
android:editorExtras setInputExtras(int) Reference to an <input-extras> XML resource containing additional data to supply to an input method, which is private to the implementation of the input method.
android:ellipsize setEllipsize(TextUtils.TruncateAt) If set, causes words that are longer than the view is wide to be ellipsized instead of broken in the middle.
android:ems setEms(int) Makes the TextView be exactly this many ems wide.
android:freezesText setFreezesText(boolean) If set, the text view will include its current complete text inside of its frozen icicle in addition to meta-data such as the current cursor position.
android:gravity setGravity(int) Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view.
android:height setHeight(int) Makes the TextView be exactly this many pixels tall.
android:hint setHint(int) Hint text to display when the text is empty.
android:imeActionId setImeActionLabel(CharSequence,int) Supply a value for EditorInfo.actionId used when an input method is connected to the text view.
android:imeActionLabel setImeActionLabel(CharSequence,int) Supply a value for EditorInfo.actionLabel used when an input method is connected to the text view.
android:imeOptions setImeOptions(int) Additional features you can enable in an IME associated with an editor to improve the integration with your application.
android:includeFontPadding setIncludeFontPadding(boolean) Leave enough room for ascenders and descenders instead of using the font ascent and descent strictly.
android:inputMethod setKeyListener(KeyListener) If set, specifies that this TextView should use the specified input method (specified by fully-qualified class name).
android:inputType setRawInputType(int) The type of data being placed in a text field, used to help an input method decide how to let the user enter text.
android:lineSpacingExtra setLineSpacing(float,float) Extra spacing between lines of text.
android:lineSpacingMultiplier setLineSpacing(float,float) Extra spacing between lines of text, as a multiplier.
android:lines setLines(int) Makes the TextView be exactly this many lines tall.
android:linksClickable setLinksClickable(boolean) If set to false, keeps the movement method from being set to the link movement method even if autoLink causes links to be found.
android:marqueeRepeatLimit setMarqueeRepeatLimit(int) The number of times to repeat the marquee animation.
android:maxEms setMaxEms(int) Makes the TextView be at most this many ems wide.
android:maxHeight setMaxHeight(int) Makes the TextView be at most this many pixels tall.
android:maxLength setFilters(InputFilter) Set an input filter to constrain the text length to the specified number.
android:maxLines setMaxLines(int) Makes the TextView be at most this many lines tall.
android:maxWidth setMaxWidth(int) Makes the TextView be at most this many pixels wide.
android:minEms setMinEms(int) Makes the TextView be at least this many ems wide.
android:minHeight setMinHeight(int) Makes the TextView be at least this many pixels tall.
android:minLines setMinLines(int) Makes the TextView be at least this many lines tall.
android:minWidth setMinWidth(int) Makes the TextView be at least this many pixels wide.
android:numeric setKeyListener(KeyListener) If set, specifies that this TextView has a numeric input method.
android:password setTransformationMethod(TransformationMethod) Whether the characters of the field are displayed as password dots instead of themselves.
android:phoneNumber setKeyListener(KeyListener) If set, specifies that this TextView has a phone number input method.
android:privateImeOptions setPrivateImeOptions(String) An addition content type description to supply to the input method attached to the text view, which is private to the implementation of the input method.
android:scrollHorizontally setHorizontallyScrolling(boolean) Whether the text is allowed to be wider than the view (and therefore can be scrolled horizontally).
android:selectAllOnFocus setSelectAllOnFocus(boolean) If the text is selectable, select it all when the view takes focus instead of moving the cursor to the start or end.
android:shadowColor setShadowLayer(float,float,float,int) Place a shadow of the specified color behind the text.
android:shadowDx setShadowLayer(float,float,float,int) Horizontal offset of the shadow.
android:shadowDy setShadowLayer(float,float,float,int) Vertical offset of the shadow.
android:shadowRadius setShadowLayer(float,float,float,int) Radius of the shadow.
android:singleLine setTransformationMethod(TransformationMethod) Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key.
android:text setText(CharSequence,TextView.BufferType) Text to display.
android:textAppearance Base text color, typeface, size, and style.
android:textColor setTextColor(int) Text color.
android:textColorHighlight setHighlightColor(int) Color of the text selection highlight.
android:textColorHint setHintTextColor(int) Color of the hint text.
android:textColorLink setLinkTextColor(int) Text color for links.
android:textIsSelectable isTextSelectable() Indicates that the content of a non-editable text can be selected.
android:textScaleX setTextScaleX(float) Sets the horizontal scaling factor for the text.
android:textSize setTextSize(int,float) Size of the text.
android:textStyle setTypeface(Typeface) Style (bold, italic, bolditalic) for the text.
android:typeface setTypeface(Typeface) Typeface (normal, sans, serif, monospace) for the text.
android:width setWidth(int) Makes the TextView be exactly this many pixels wide.