//显示文字 Toast.makeText(this,"Toast显示文本",Toast.LENGTH_SHORT).show(); //显示图片 Toast toast = new Toast(this); ImageView imageView = new ImageView(this); imageView.setImageResource(R.mipmap.ic_launcher); toast.setView(imageView); toast.show();
//图文 Toast toast = new Toast(this); TextView textView = new TextView(this); textView.setText("显示带图片的toast"); ImageView imageView = new ImageView(this); //设置图片 imageView.setImageResource(R.mipmap.ic_launcher); //得到Toast的布局 LinearLayout layout = new LinearLayout(this); //设置此布局为横向的 layout.setOrientation(LinearLayout.VERTICAL); //将ImageView再加入到此布局中的第一个位置 layout.addView(imageView); //将TextView放到第二个位置 layout.addView(textView); toast.setView(layout); toast.setGravity(Gravity.CENTER,0,0); toast.setDuration(Toast.LENGTH_SHORT); toast.show();
/** * TextView * 向用户显示文本和选择性的容许编辑他。一个TextView是一个完整的文本编辑器,可是基本的类配置为 * 不容许编辑;容许用户赋值TextView价值部分或前部粘贴到其余地方,设置XML属性 * android:textIsSelectable为true或者叫textView.setTextIsSelectable(true)。 * textIsSelectable标记TextView容许用户作出选择的手势,进而触发系统内置的复制粘贴控制 */
TextView属性大全:android
android:autoLink设置是否当文本为URL连接/email/电话号码/map时,文本显示为可点击的连接。可选值(none/web/email/phone/map/all) android:autoText若是设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起做用。 android:bufferType指定getText()方式取得的文本类别。选项editable 相似于StringBuilder可追加字符, 也就是说getText后可调用append方法设置文本内容。spannable 则可在给定的字符区域使用样式 android:capitalize设置英文字母大写类型。此处无效果,须要弹出输入法才能看获得,参见EditView此属性说明。 android:cursorVisible设定光标为显示/隐藏,默认显示。 android:digits设置容许输入哪些字符。如“1234567890.+-*/% ()” android:drawableBottom在text的下方输出一个drawable,如图片。若是指定一个颜色的话会把text的背景设为该颜色,而且同时和background使用时覆盖后者。 android:drawableLeft在text的左边输出一个drawable,如图片。 android:drawablePadding设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一块儿使用,可设置为负数,单独使用没有效果。 android:drawableRight在text的右边输出一个drawable。 android:drawableTop在text的正上方输出一个drawable。 android:editable设置是否可编辑。 android:editorExtras设置文本的额外的输入数据。 android:ellipsize设置当文字过长时,该控件该如何显示。有以下值设置:”start”—?省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间; ”marquee” ——以跑马灯的方式显示(动画横向移动) android:freezesText设置保存文本的内容以及光标的位置。 android:gravity设置文本位置,如设置成“center”,文本将居中显示。 android:hintText为空时显示的文字提示信息,可经过textColorHint设置提示信息的颜色。此属性在EditView中使用,可是这里也能够用。 android:imeOptions附加功能,设置右下角IME动做与编辑框相关的动做,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditView中再详细 说明,此处无用。 android:imeActionId设置IME动做ID。 android:imeActionLabel设置IME动做标签。 android:includeFontPadding设置文本是否包含顶部和底部额外空白,默认为true。 android:inputMethod为文本指定输入法,须要彻底限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,可是这里报错找不到。 android:inputType设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中再详细说明,这里无效果。 android:linksClickable设置连接是否点击链接,即便设置了autoLink。 android:marqueeRepeatLimit在ellipsize指定marquee的状况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。 android:ems设置TextView的宽度为N个字符的宽度。这里测试为一个汉字字符宽度 android:maxEms设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。 android:minEms设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。 android:maxLength限制显示的文本长度,超出部分不显示。 android:lines设置文本的行数,设置两行就显示两行,即便第二行没有数据。 android:maxLines设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。 android:minLines设置文本的最小行数,与lines相似。 android:lineSpacingExtra设置行间距。 android:lineSpacingMultiplier设置行间距的倍数。如”1.2” android:numeric若是被设置,该TextView有一个数字输入法。此处无用,设置后惟一效果是TextView有点击效果,此属性在EdtiView将详细说明。 android:password以小点”.”显示文本 android:phoneNumber设置为电话号码的输入方式。 android:privateImeOptions设置输入法选项,此处无用,在EditText将进一步讨论。 android:scrollHorizontally设置文本超出TextView的宽度的状况下,是否出现横拉条。 android:selectAllOnFocus若是文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。TextView中设置后无效果。 android:shadowColor指定文本阴影的颜色,须要与shadowRadius一块儿使用。 android:shadowDx设置阴影横向坐标开始位置。 android:shadowDy设置阴影纵向坐标开始位置。 android:shadowRadius设置阴影的半径。设置为0.1就变成字体的颜色了,通常设置为3.0的效果比较好。 android:singleLine设置单行显示。若是和layout_width一块儿使用,当文本不能所有显示时,后面用“…”来表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"将只显示“t…”。若是不设置singleLine或者设置为false,文本将自动换行 android:text设置显示文本. android:textAppearance设置文字外观。如“?android:attr/textAppearanceLargeInverse”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,不然使用默认的 外观。可设置的值以下: textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmal l/textAppearanceSmallInverse android:textColor设置文本颜色 android:textColorHighlight被选中文字的底色,默认为蓝色 android:textColorHint设置提示信息文字的颜色,默认为灰色。与hint一块儿使用。 android:textColorLink文字连接的颜色. android:textScaleX设置文字之间间隔,默认为1.0f。 android:textSize设置文字大小,推荐度量单位”sp”,如”15sp” android:textStyle设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 能够设置一个或多个,用“|”隔开 android:typeface设置文本字体,必须是如下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3] android:height设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米) android:maxHeight设置文本区域的最大高度 android:minHeight设置文本区域的最小高度 android:width设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),与layout_width的区别看这里。 android:maxWidth设置文本区域的最大宽度 android:minWidth设置文本区域的最小宽度
1、EditText输入的文字为密码形式的设置 (1)经过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 (2)在代码里设置: 经过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码。 editText.setTransformationMethod(PasswordTransformationMethod.getInstance());//设置密码为不可见。 二、(1)EditText输入的文字为电话号码 Android:phoneNumber=”true” //输入电话号码 3、EditText字数限制的设置 (1)在.xml中设置:android:maxLength=“50” (2)代码中设置: editText.setFilters(new InputFilter[]{newInputFilter.LengthFilter(100)}); 4、EditText设置字体 android:typeface="monospace" //设置字型。字形有:normal, sans, serif,monospace 5、EditText是否可编辑 Android:editable // 是否可编辑 6、在EditText中软键盘的调起、关闭 (1)EditText有焦点(focusable为true)阻止输入法弹出 editText=(EditText)findViewById(R.id.txtBody); editText.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event){ editText.setInputType(InputType.TYPE_NULL); //关闭软键盘 return false; } }); (2)当EidtText无焦点(focusable=false)时阻止输入法弹出 InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(),0); (3)调用数字键盘并设置输入类型和键盘为英文 etNumber.setInputType(InputType.TYPE_CLASS_NUMBER); //调用数字键盘 rlEditText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);//设置输入类型和键盘为英文 或者:android:inputType="textUri|textMultiLine" (4)android:focusable="false"//键盘永远不会弹出 <activity android:name=".AddLinkman"android:windowSoftInputMode="adjustUnspecified|stateHidden"/>//不自动弹出键盘 //关闭键盘(好比输入结束后执行) InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(etEditText.getWindowToken(), 0); //自动弹出键盘 ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS); etEditText.requestFocus();//让EditText得到焦点,可是得到焦点并不会自动弹出键盘 7、android:layout_gravity和android:gravity的区别 (1)android:layout_gravity是本元素对父元素的重力方向。 (2)android:gravity是本元素全部子元素的重力方向。 8、android:padding和android:layout_margin区别 这两个均可以设置边距,但有细微的区别: (1)android:padding是相对父view的边距 (2)android:layout_margin是相对同一级View的边距 例:LinearLayout是水平布局,下面有两个按钮, (a)若是右边的按钮想距左边的按钮15px,由于这两个按钮是同一级的,应该用android:layout_margin; (b)若是右边的按钮想距左边的距离为350px,应该用android:padding 九、android:numeric//只接受数字 android:numeric来控制输入的数字类型,一共有三种分别为integer(正整数)、signed(带符号整数,有正负)和decimal(浮点数)。 10、Enter键图标的设置 软键盘的Enter键默认显示的是“完成”文本,咱们知道按Enter建表示前置工做已经准备完毕了,要去什么什么啦。好比,在一个搜索中,咱们输入要搜索的文本,而后按Enter表示要去搜索了,可是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,若是能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证实咱们的想法是合理的,Android也为咱们提供的这样的功能。经过设置android:imeOptions来改变默认的“完成”文本。这里举几个经常使用的常量值: (1)actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED效果: (2)actionNone 没有动做,对应常量EditorInfo.IME_ACTION_NONE效果: (3)actionGo去往,对应常量EditorInfo.IME_ACTION_GO 效果: (4)actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH效果: (5)actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND效果: (6)actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT效果: (7)actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE效果: 11、使用android:imeOptinos可对Android自带的软键盘进行一些界面上的设置: android:imeOptions="flagNoExtractUi" //使软键盘不全屏显示,只占用一部分屏幕 同时,这个属性还能控件软键盘右下角按键的显示内容,默认状况下为回车键 android:imeOptions="actionNone" //输入框右侧不带任何提示 android:imeOptions="actionGo" //右下角按键内容为'开始' android:imeOptions="actionSearch" //右下角按键为放大镜图片,搜索 android:imeOptions="actionSend" //右下角按键内容为'发送' android:imeOptions="actionNext" //右下角按键内容为'下一步' android:imeOptions="actionDone" //右下角按键内容为'完成' 12、限定edittext能输入数字和字母,而且默认输入为数字,如身份证号码 android:inputType="number" android:digits="0123456789xyzXYZ" 13、软键盘的调起致使原来的界面被挤上去,或者致使界面下面的tab导航被挤上去,解决方法以下 解决方法: 使用Manifest中的Activity的android:windowSoftInputMode的"adjustPan"属性。 另外注意:有关软键盘的问题可参考android:windowSoftInputMode中属性。 1四、edittext光标详解 edittext.requestFocusFromTouch();//让光标放入到点击位置。 edittext.requestFocus();//默认方式得到焦点 EditText editor = (EditText)getCurrentView();//光标处插入 int cursor = editor.getSelectionStart(); editor.getText().insert(cursor,delta); 让光标移到末端(这样文字就会向前显示) EditText et = ... String text = "text"; et.setText(text); et.setSelection(text.length()); android:cursorVisible="false" 隐藏光标 android:background="#00000000"//不要文本框背景 Android - 文本框的输入法控制和默认焦点设置 在开发中,必不可少的会使用到文本框(EditText)来进行数据录入,也就会须要对输入法进行一些控制。 先看下LAYOUT定义文件中的和输入法有关的属性: 属性名 说明 android:inputType 指定输入法的类型,int类型,能够用|选择多个。取值能够参考:android.text.InputType类。取值包括:text, textUri, phone,number,等。 android:imeOptions 指定输入法窗口中的回车键的功能,可选值为normal, actionNext,actionDone,actionSearch等。部分输入法对此的支持可能不够好。 下面的LAYOUT定义文件举了一些例子说明inputType和imeOptions的使用。 <EditText android:id="@+id/textNormal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Normal text" android:inputType="text" android:imeOptions="actionNext" /> <EditText android:id="@+id/textInteger" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Integer only" android:inputType="number" android:imeOptions="actionNext" /> <EditText android:id="@+id/textPhone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Phone number" android:inputType="phone" android:imeOptions="actionNext" /> <EditText android:id="@+id/textEmail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Email" android:imeOptions="actionSend" android:inputType="textEmailAddress" /> <EditText android:id="@+id/textSite" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Web Site" android:imeOptions="actionDone" android:inputType="textUri" /> 有时候也要对intent的默认焦点进行设置,不至于在intent跳转的时候默认焦点(光标)在EditText上,致使进入intent就打开输入法,影响界面美观。 默认焦点的顺序是:从上倒下 从左到右第一个能够输入的控件做为焦点 可使用: button.setFocusable(true); button.requestFocus(); button.setFocusableInTouchMode(true); 也能够: 在EditText前面放置一个看不到的LinearLayout,让他率先获取焦点: <LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px"/> Android EditText 属性汇总 Android EditText 属性汇总 android:layout_gravity="center_vertical" 设置控件显示的位置:默认top,这里居中显示,还有bottom android:hint="请输入数字!" 设置显示在空间上的提示信息 android:numeric="integer" 设置只能输入整数,若是是小数则是: decimal android:singleLine="true" 设置单行输入,一旦设置为true,则文字不会自动换行。 android:password="true" 设置只能输入密码 android:textColor = "#ff8c00" 字体颜色 android:textStyle="bold" 字体,bold, italic, bolditalic android:textSize="20dip" 大小 android:capitalize = "characters" 以大写字母写 android:textAlign="center" EditText没有这个属性,但TextView有 android:textColorHighlight="#cccccc" 被选中文字的底色,默认为蓝色 android:textColorHint="#ffff00" 设置提示信息文字的颜色,默认为灰色 android:textScaleX="1.5" 控制字与字之间的间距 android:typeface="monospace" 字型,normal, sans, serif, monospace android:background="@null" 空间背景,这里没有,指透明 android:layout_weight="1" 权重,控制控件之间的地位,在控制控件显示的大小时蛮有用的。 android:textAppearance="?android:attr/textAppearanceLargeInverse" 文字外观,这里引用的是系统自带的一个外观,?表示系统是否有这种外观,不然使用默认的外观。不知道这样理解对不对? 经过EditText的layout xml文件中的相关属性来实现: 1. 密码框属性 android:password="true" 这条可让EditText显示的内容自动为星号,输入时内容会在1秒内变成*字样。 2. 纯数字 android:numeric="true" 这条可让输入法自动变为数字输入键盘,同时仅容许0-9的数字输入 3. 仅容许 android:capitalize="cwj1987" 这样仅容许接受输入cwj1987,通常用于密码验证 下面是一些扩展的风格属性 android:editable="false" 设置EditText不可编辑 android:singleLine="true" 强制输入的内容在单行 android:ellipsize="end" 自动隐藏尾部溢出数据,通常用于文字内容过长一行没法所有显示时
/** * Button * 1.经常使用属性 * 二、OnClickListener事件 * (1)本类实现View.OnClickListener接口 * (2)使用内部类 * (3)自定义方法,配置android:onclick属性 * 三、设置透明的Button * style="?android:attr/borderlessButtonStyle" * 四、配置Button style * 经过配置android:background来设置按钮样式 * 使用XML drawable selector * (1)首先在drawable中新建一个drawable resource file命名为bg_button(即selector) * 在里面设置按钮点击与没被点击时的背景 * (2)在xml里经过background设置 * android:background="@drawable/bg_button"便可 */
Button点击事件:git
方法一:web
<Button android:id="@+id/btn1" android:onClick="Select" android:text="Select" android:layout_width="wrap_content" android:layout_height="wrap_content" />
public void Select(View v) { Toast.makeText(this,"点击事件",Toast.LENGTH_SHORT).show(); }
方法二(使用内部类的方式):api
private Button btn1; btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"点击事件",Toast.LENGTH_SHORT).show(); } });
Button设置样式app
bg_button.xml框架
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/colorAccent" android:state_pressed="true"></item> <item android:drawable="@color/colorPrimary" android:state_pressed="false"></item> </selector>
<Button android:id="@+id/btn1" android:onClick="Select" android:text="Select" android:background="@drawable/bg_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
/** * ImageView * XML属性 * android:src:设置View的drawable(如图片,也能够是颜色,可是须要指定View的大小) * android:adjustViewBounds:是否保持宽高比。须要与maxWidth、maxHeight一块儿使用,单独使用没有效果 * android:maxHeight:设置View的最大高度,单独使用无效,须臾setAdjustViewBounds一块儿使用 * 若是想设置图片固定大小,邮箱保持图片宽高比,须要以下设置: * (1)设置setAdjustViewBounds为true * (2)设置maxHeight,maxWidth * (3)设置layout_width和layout_height为wrap_content */
/* * scaleType属性 * android:scaleType属性,设置图片的填充方式,有以下常量值: * matrix 用矩阵来绘制,动态缩小放大图片来显示 * fitXY 把图片不按比例扩大/缩小到View的大小限时 * fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置 * fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示 * fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置 * center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示 * centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽) * centerInside 将图片的内容完整居中显示,经过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽 * android:hint 将图片渲染成指定的颜色 */
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private CheckBox checkBox1; private CheckBox checkBox2; private CheckBox checkBox3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * CheckBox(复选框) * 复选框容许用户从一组选择中选择一个或多个,设置选中和未选中可用setChecked(),参数为true或false */ checkBox1 = (CheckBox) findViewById(R.id.checkbox_id1); checkBox2 = (CheckBox) findViewById(R.id.checkbox_id2); checkBox3 = (CheckBox) findViewById(R.id.checkbox_id3); checkBox1.setOnCheckedChangeListener(this); checkBox2.setOnCheckedChangeListener(this); checkBox3.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { switch (compoundButton.getId()) { case R.id.checkbox_id1: if (b) { Toast.makeText(this, "你选中了小说", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "你取消了小说", Toast.LENGTH_SHORT).show(); } break; case R.id.checkbox_id2: if (b) { Toast.makeText(this, "你选中了电影", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "你取消了电影", Toast.LENGTH_SHORT).show(); } break; case R.id.checkbox_id3: if (b) { Toast.makeText(this, "你选中了游戏", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "你取消了游戏", Toast.LENGTH_SHORT).show(); } break; } } }
/** * RadioButton(单选按钮) * 容许用户从一组选择中选择一个选项,必须用RadioGroup将他们分组在一块儿,才能确保只能选择一项 * 若国祥让RadioButton横着排,则在RadioGroup里设置android:orientation="horizontal" * 事件处理:onRadioButtonClicked */ radioButton1 = (RadioButton) findViewById(R.id.radioButton1); radioButton2 = (RadioButton) findViewById(R.id.radioButton2); radioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ Toast.makeText(MainActivity.this,"你选择了男",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this,"你取消选择了男",Toast.LENGTH_SHORT).show(); } } });
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用,RadioGroup是单选组合框,能够容纳多个RadioButton的容器,在没有RadioGroup的状况下,RadioButton能够全都选中,当多个RadioButton被RadioGroup包含的状况下,RadioButton只能选择一个,less
并用setOnCheckedChangeListener来对单选按钮进行监听编辑器
RadioGroup相关属性:ide
RadioGroup.getCheckedRadioButtonId();//获取选中按钮的id
RadioGroup.clearCheck();
//清除选中状态
RadioGroup.check(int id);函数
经过参入选项id来设置该选项为选中状态,若是传递-1做为指定的选择标识符来清除单选按钮组的勾选状态,至关于调用clearCheck()操做
setOnCheckedChangeListener(RadioGroup.OnCkeckedChangeListener listener);//一个当该单选按钮组中的单选按钮勾选状态发生改变时所要调用的回调函数
addView (View child,int index,ViewGroup.layoutParams params);
使用指定的布局参数添加一个子视图,参数 child 所要添加的子视图,index 将要添加子视图的位置,params 所要添加的子视图的布局参数
RadioButton.getText();
//获取单选框的值
能够经过设置以下属性可使单选按钮在显示文本的右边
android:button="@null" //去除RadioButton 前面的圆点
android:drawableRight="@android:drawable/btn_radio"
调用用adioButton.setButtonDrawable(android.R.color.transparent);
在JAVA代码中:
去除RadioButton前面的圆点adioButton.setButtonDrawable(android.R.color.transparent);
给ListView设置分割线,只需设置以下两个属性:
android:divider="#000" //设置分割线显示颜色
android:dividerHeight="1px" //此处非0,不然无效
Android (shape,gradient)使用总结
设置背景色能够经过在res/drawable里定义一个xml,以下:
<shape> <!-- 实心 --> <solid android:color="#ff9d77"/> <!-- 渐变 --> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" android:angle="270" /> <!-- 描边 --> <stroke android:width="2dp" android:color="#dcdcdc" /> <!-- 圆角 --> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
shape是用来定义形状的,
solid:实心,android:color指填充颜色
gradient定义该形状里面为渐变色填充,startColor其实颜色,endColor结束颜色,angle是渐变角度,必须为45的整数倍,当angle=0时,渐变色是从左到右,angle=90时,从下往上
stroke:描边
android:width描边的宽度,android:color描边的颜色
咱们还能够把描边弄成虚线形式:android:dashWidth横线的宽度,android:dashGap表示间隔开的距离
corners:圆角
android:radius为教的弧度,值越大角越圆
<corners android:topRightRadius="20dp" 右上角 android:bottomLeftRadius="20dp" 右下角 android:topLeftRadius="1dp" 左上角 android:bottomRightRadius="0dp" 左下角 />
/** *ToggleButton(Switch用法与ToggleButton同样) * 一个切换按钮容许用户改变二者之间的设置 *经过android:textOff和android:textOn自定义显示的文字 */ toggleButton = (ToggleButton) findViewById(R.id.toggleButton); aSwitch = (Switch) findViewById(R.id.switch1); aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) { Toast.makeText(MainActivity.this, "打开", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "关", Toast.LENGTH_SHORT).show(); } } });
<RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="6" android:stepSize="0.5" android:rating="3.5" android:id="@+id/rating"/>
/** *RatingBar * RatingBar是基于SeekBar和ProgressBar的扩展,用星型来显示等级评定。使用RatingBar的默认大小是,用户能够触摸、 * 拖动或使用键来设置评分 * 有两种样式(小峰哥用rationgBar,大风格用rationgBarStylendicator),其中大的只适合知识,不适合与用户交互 * 当使用能够与用户交互的rationgbar时,只能把布局的宽设置为wra_content,设置的星星数量(经过函数 * setNumStars(int)或者在XML的布局文件重定义)将显示出来(若是设置为另外一种布局款的话,后果没法预知) * 事件处理接口:RatingBar.OnRationgBarChangeLitener * 一个回调函数,当星级进度改变时修改客户端的星级。 * android:numStars="6"设置星星的总数 * android:stepSize每次增加或减小的步长 * android:rating默认选中的星数 * 若设置android:isIndicator为true,则不能与用户交互,只能显示 */ ratingBar = (RatingBar) findViewById(R.id.rating); ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float v, boolean b) { //b:是用户选的仍是默认的,用户true //v:是当前选中的星星数量 if (b){ Toast.makeText(MainActivity.this,"rationg="+v,Toast.LENGTH_SHORT).show(); } } });