1. TextView使用
常见单位介绍:
- dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。
- px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。
- pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用
- sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。
该控件主要用于界面上显示一段文本信息。默认居左上角对齐
1.1 主要属性介绍
- id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置
- layout_width:组件的宽度,一般写:wrap_content或者match_parent(fill_parent),前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器;当然也可以设置成特定的大小
- layout_height:组件的高度,内容同上
- gravity:设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等
- text:设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容的
- textColor:设置字体颜色,同上,通过colors.xml资源来引用
- textStyle:设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体)
- textSize:字体大小,单位一般是用sp!
- background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片哦!
示例:
<TextView
android:id="@+id/txtOne"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:text="TextView(显示框)"
android:textColor="#EA5246"
android:textStyle="bold|italic"
android:background="#000000"
android:textSize="18sp" />
1.2 常见的开发案例:
1.2.1 带阴影的TextView
- android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用
- android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
- android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
- android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置
1.2.2 带边框的TextView
为TextView加边框。须要在drawable建xml文件,里面设置shape来设置文本框的特殊效果,然后将background设置为这个drawable资源。
- <solid android:color = "xxx"> 这个是设置背景颜色的
- <stroke android:width = "xdp" android:color="xxx"> 这个是设置边框的粗细,以及边框颜色的
- <padding androidLbottom = "xdp"...> 这个是设置边距的
- <corners android:topLeftRadius="10px"...> 这个是设置圆角的
- <gradient> 这个是设置渐变色的,可选属性有: startColor:起始颜色 endColor:结束颜色 centerColor:中间颜色 angle:方向角度,等于0时,从左到右,然后逆时针方向转,当angle = 90度时从下往上 type:设置渐变的类型
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置透明背景色 -->
<solid android:color="#87CEEB" />
<!-- 设置一个黑色边框 -->
<stroke
android:width="2px"
android:color="#000000" />
<!-- 设置四个圆角的半径 -->
<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />
<!-- 渐变 -->
<gradient
android:angle="270"
android:endColor="#C0C0C0"
android:startColor="#FCD209" />
<!-- 设置一下边距,让空间大一点 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
1.2.3 带图片(drawableXxx)的Textview
[图片上传失败...(image-a3bc94-1565577348777)]
-
要实现上面这种功能,可以使用drawableXxx可以省略上面过程,可以设置四个方向的图片drawableTop(上), drawableButtom(下), drawableLeft(左), drawableRight(右) 。另外,你也可以使用drawablePadding来设置图片与文字间的间距。
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:drawableTop="@drawable/show1" android:drawablePadding="10dp" android:text="发现" />
-
在XML中是无法直接设置drawable大小,需要在Java代码中进行修改。
public class MainActivity extends AppCompatActivity {
private TextView txtZQD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtZQD = (TextView) findViewById(R.id.txtZQD);
Drawable[] drawable = txtZQD.getCompoundDrawables();
// 数组下表0~3,依次是:左上右下
drawable[1].setBounds(100, 0, 200, 200);
txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2],
drawable[3]);
}
- ①Drawable[] drawable = txtZQD.getCompoundDrawables( ); 获得四个不同方向上的图片资源,数组元素依次是:左上右下的图片
- ②drawable[1].setBounds(100, 0, 200, 200); 接着获得资源后,可以调用setBounds设置左上右下坐标点,比如这里设置了代表的是: 长是:从离文字最左边开始100dp处到200dp处 宽是:从文字上方0dp处往上延伸200dp!
- ③
txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2], drawable[3]);
为TextView重新设置drawable数组!没有图片可以用null代替哦! PS:另外,从上面看出我们也可以直接在Java代码中调用setCompoundDrawables为 TextView设置图片!
1.2.4 使用autoLink属性识别链接类型
当文字中出现了URL,E-Mail,电话号码,地图的时候,我们可以通过设置autoLink属性;当我们点击 文字中对应部分的文字,即可跳转至某默认APP,比如一串号码,点击后跳转至拨号界面!
<TextView
android:id="@+id/text_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/txt_radiuborder"
android:text="www.baidu.com"
android:textSize="30sp"
android:autoLink="web"
/>
1.2.5 跑马灯TextView
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
在XML定义了上述属性,还需要在java代码中进行程序控制。
TextView mTextView = (TextView) findViewById(R.id.myTextView);
// 开始走马灯效果
mTextView.setSelected(true);
// 停止走马灯效果
// mTextView.setSelected(false);
1.2.6 设置TextView字间距与行间距
字间距:
android:textScaleX:控制字体水平方向的缩放,默认值1.0f,值是float
Java中setScaleX(2.0f);
行间距: Android系统中TextView默认显示中文时会比较紧凑,为了让每行保持的行间距
android:lineSpacingExtra:设置行间距,如"3dp" android:lineSpacingMultiplier:设置行间距的倍数,如"1.2"
Java代码中可以通过: setLineSpacing方法来设置
1.2.7 自动换行
自动换行通过 android:singleLine 设置,默认为 false。
如需要自动换行,可以用:
android:singleLine = "false"
如果要在一行显示完,不换行,可以用:
android:singleLine = "true"
除此之外,可以也设置多行显示不完,添加个maxLines的属性即可!
2. EditText使用
改控件允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理。例如:短信、微信、QQ等操作。
如前述控件所示,在xml文件中定义该控件。
-
Android中的控件规律:给控件定义一个id,再指定控件的宽度和高度,再适当加入一些控件特有的属性。
-
android:hit:输入框中如果想显示一些提示性的文字,可以使用该属性来指定提示的内容
-
如果输入的内容过多会导致EditText不断拉长,界面变丑,可以通过使用android:maxLines属性指定最大行数,输入内容超过两行,文本向上滚动。
-
EditText可以和Button相结合完成一些功能
-
示例:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); editText = (EditText) findViewById(R.id.edit_text); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: String inputText = editText.getText().toString(); Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show(); break; default: break; } } }
2.1 全选文本与限制输入类型
- 点击输入框,获得焦点,获得输入框全部本文内容使用
android:selectAllOnFocus
:
android:selectAllOnFocus="true"
- 限制输入类型可以通过inputType属性来实现
android:inputType="phone"
-
android:capitalize:提供英文字母大写类型
- sentences:仅第一个字母大写
- words:每一个单词首字母大小,用空格区分单词
- characters:每一个英文字母都大写
-
android:digits:
设置允许输入哪些字符,如"1234567890.+-*/%\n()"
-
android:cursorVisible
设定光标为显示/隐藏,默认显示,设置为false,即使选中也不显示
-
android:textCursorDrawable:
设置光标样式
网友评论