`
836811384
  • 浏览: 546043 次
文章分类
社区版块
存档分类
最新评论

android 动态添加自定义TextView

 
阅读更多
// 设置背景图
textView.setBackgroundResource(R.drawable.block_text_backgroumg);
// 设置背景透明度
textView.getBackground().setAlpha(150);
// 设定text内容为Html格式
textView.setText(Html.fromHtml(rsultText));
// 设定为可以scroll的textView
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
// 设定text内容与边框的距离
textView.setPadding(6, 6, 6, 6);
// 添加textView到Layout
mLytMain.addView(textView, textParams);

注意点:
1.----------------------------------
因为在android中TextView是没有边框的,为了添加边框效果,有如下两种方案,
1. 重写TextView类 2.利用.9.png图像来制作一个有边框的背景。(上边的例子使用了第二种方法)
参考网址:
Android学习系列(4)--App自适应draw9patch不失真背景
http://www.cnblogs.com/qianxudetianxia/archive/2011/04/17/2017591.html
Android Nine Patch图片及按钮背景
http://www.cnblogs.com/feisky/archive/2010/01/16/1649502.html

2.----------------------------------
在上面的例子中虽然已经设定为可以滚动的文本,但不会有滚动条显示,这是可以借助ScrollView。
参考网址:http://wangjun.easymorse.com/?p=255

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:scrollbars="vertical" android:fadingEdge="vertical">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/description1"
        android:textColor="#071907" android:paddingTop="5dip" />
</ScrollView>

例外一种实现方式也是在布局文件中。android:scrollbars="vertical"
**在动态创建的时候有一个方法:setVerticalScrollBarEnabled,可在测试时没有达到想要的效果。
3.----------------------------------
开发的过程中遇到一个问题,默认TextView的最后一行不显示,而且左边有很大一块也无法显示。
查到如下网址:http://znwa1m5y.blog.sohu.com/202620579.html
他是在布局文件中设定而非添加,解决办法是将 TextView中 android:layout_width="fill_parent" 改为 wrap_content。
我试着在动态添加时在LayoutParams设定为wrap_content,可是没有达到效果。
通过textView.setPadding(6, 6, 6, 6);解决
4.----------------------------------
我们还可以给文本框设定单行显示和多行显示的最大行数
android:singleLine="true" <!--实现单行 --> (默认即为多行)
android:maxLines="15" <!--最多不超过15行 -->
5.----------------------------------
关于EditText的多行文本显示
在Android开发中,多行文本框EditText的默认显示方式是居中,那怎么让它从第一行开始显示呢?
只需要在EditText的属性中加上 android:gravity=”top” 即可。
或者通过编程的方式动态实现:
private EditText body;
body=(EditText)findViewById(R.id.main_body);
body.setGravity(Gravity.TOP);
其中函数setGravity()的参数是一个int,常见的可选值为 Gravity.TOP,Gravity.BOTTOM,
Gravity.LEFT和Gravity.RIGHT 。
6.----------------------------------
在html中设定的字体大小,在TextView中显示时无效!!!
查资料得知:<font size=3> 数值设定从1到7。另外还有一种是在控件中通过style属性来设定字体大小,
测试了一下<p style="font-size:20pt">测试字体大小</p>也是没有作用,那要如何设置呢。
想不到好的办法,只好解析html语言,返回size,通过setTextSize()来进行设定。
7.----------------------------------
如果不是通过html,而要动态设定textview中显示的字体为粗体和斜体,要如何做呢?
text=new TextView(this);

// 设置字型为默认,正常字体
text.setTypeface(Typeface.DEFAULT,Typeface.NORMAL);
// 设置字型为默认粗体,粗体字体
text.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
// 设置字型为等宽字型,斜体字体
text.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC);
// 设置字型为等宽字型,粗斜体字体
text.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
// 针对中文仿“粗体”
text.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
//使用TextPaint的仿“粗体”设置setFakeBoldText为true。目前还无法支持仿“斜体”方法
TextPaint tp = chinese.getPaint();
tp.setFakeBoldText(true);
// 自定义字体
//字体MgOpenCosmeticaBold.ttf放置于assets/font/路径下
Typeface typeface=Typeface.createFromAsset(getAssets(),"font/MgOpenCosmeticaBold.ttf");
text.setTypeface(typeface);


8.----------------------------------
Android中TextVIew一些属性

android:layout_gravity="center_vertical"
设置控件显示的位置:默认top,这里居中显示,还有bottom

android:hint="请输入数字!"
设置显示在空间上的提示信息

android:numeric="integer"
设置只能输入整数,如果是小数则是:decimal

android:singleLine="true"
设置单行输入,一旦设置为true,则文字不会自动换行。

android:password="true"
设置只能输入密码

android:gravity="top"
EditText设置,这一行就可以让光标处于第一行了,若不设置默认就居中
TextView则在最顶上

android:textColor = "#ff8c00"
字体颜色

android:textStyle="bold"
字体,bold, italic, bolditalic

android:textSize="20dip"
大小

android:capitalize = "characters"
以大写字母写

android:textAlign="center"
EditText没有这个属性,但TextView有

android:autoText:自动拼写帮助

android:editable:是否可编辑

android:textColorHighlight="#cccccc"
被选中文字的底色,默认为蓝色

android:textColorHint="#ffff00"
设置提示信息文字的颜色,默认为灰色

android:textScaleX="1.5"
控制字与字之间的间距

android:typeface="monospace"
字型,normal, sans, serif, monospace

android:background="@null"
空间背景,这里没有,指透明,将EditText自定义的背景去掉

android:imeOptions="actionDone"
设置软键盘的Enter键

android:layout_weight="1"
权重,控制控件之间的地位,在控制控件显示的大小时蛮有用的。

android:textAppearance="?android:attr/textAppearanceLargeInverse"
文字外观,这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。不知道这样理解对不对?


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics