上一篇学习了ApiDemos的启动Activity的知识点,本篇学习其中Text选项的知识点,先看一下截图
先看第一个Linkify
此功能中,主要是给Text文本添加超链接,先看一下android的ApiDemos中原型怎么写的
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showDividers="middle"
android:divider="?android:attr/listDivider">
// 通过在tv1的xml文件中为其设置android:autoLink="all"属性使tv1中的文本自动按系统预定规则设置超链接
<TextView android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:autoLink="all"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/link_text_auto"
/>
<TextView android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/link_text_manual"
/>
<TextView android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView android:id="@+id/text4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
</ScrollView>
这是一个ScrollView布局,
public class Link extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.link);
// 在String.xml中使用<a></a>来标识超链接的文本
TextView t2 = (TextView) findViewById(R.id.text2);
// 为超链接创建进入接口
t2.setMovementMethod(LinkMovementMethod.getInstance());
// 通过代码设置超链接的内容
TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(
Html.fromHtml(
"<b>text3: Constructed from HTML programmatically.</b> Text with a " +
"<a href=\"http://www.google.com\">link</a> " +
"created in the Java source code using HTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());
//SpannableString给text添加下划线或者栅格线等效果
SpannableString ss = new SpannableString(
"text4: Manually created spans. Click here to dial the phone.");
//为第1-30个字符设置加粗
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 30,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 31+6, 31+10,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
}
}
超链接的Text就完事儿啦。
第二个是个自定义的继承于TextView的控件LogTextBox
先看下LogTextBox的自定义类
public class LogTextBox extends TextView {
public LogTextBox(Context context) {
this(context, null);
}
public LogTextBox(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
public LogTextBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected MovementMethod getDefaultMovementMethod() {
return ScrollingMovementMethod.getInstance();
}
@Override
public Editable getText() {
return (Editable) super.getText();
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, BufferType.EDITABLE);
}
}
<attr name="textViewStyle" format="reference" />
LogTextBox1的Activity
public class LogTextBox1 extends Activity {
private LogTextBox mText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_text_box_1);
mText = (LogTextBox) findViewById(R.id.text);
Button addButton = (Button) findViewById(R.id.add);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mText.append("This is a test\n");
} });
}
}
可以通过点击按钮向自定义的text中添加文本
第三个Marquee,实在不知道是什么原因,并没有写出来调用方法,在这里就把它的布局文件写出来吧
public class Marquee extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marquee);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="@string/marquee_default"
android:singleLine="true"
android:ellipsize="marquee"/>
<Button
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="@string/marquee_once"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="1"/>
<Button
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="@string/marquee_forever"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"/>
</LinearLayout>