目录
TextView属性:
例子:
创建一个应用程序,命名为:E01_TextView01,空的Activity(Blank Activity),把布局文件修改成线性布局(LinearLayout):
2.在布局文件添加一个按钮,用来跳转到TextView,然后新建一个用来显示TextView的Activity.所有演示的TextView都放在这个Activity.
自动生成一个Activity和一个布局文件,且在Manifest.xml中已经注册好了这个Activity:
在activituy_text_view.xml修改文本的属性:
1.字体大小、颜色
2.字体过多显示不下用 .....表示
3.中划线、下划线
(注意:这两个属性需要在Activity中获取TextView控件对象,然后设置控件方法才可以显示)
4.字体滚动
贴出布局文件和对应Activity的代码:
1.activity_main.xml代码,就一个按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="@+id/buttonId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到TextView"
android:textAllCaps="false"/>
</LinearLayout>
对应Activity的代码(MainActivity.java)代码,为按钮绑定监听器,在点击事件中启动另一个Activity:
package com.example.yuan.e01_textview02;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonId);
//为按钮设置一个点击事件,使用匿名内部类的方法
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//跳转到TextView演示界面
//新建一个 Intent对象,第一个参数是从 哪个Activity跳转,第二个是跳转到哪里
Intent intent = new Intent(MainActivity.this,TextViewActivity.class);
//启动这个意图
startActivity(intent);
}
});
}
}
2.activity_text_view.xml的代码,几个TextView的属性全部设置在这个布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="#000000"
android:textSize="24sp"/>
<TextView
android:id="@+id/tv_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:text="Hello World"
android:textColor="#000000"
android:textSize="24sp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tv_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="#000000"
android:textSize="24sp"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/tv_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="#000000"
android:textSize="24sp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tv_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="#000000"
android:textSize="24sp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tv_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello WorldHello WorldHello WorldHello WorldHello World"
android:textColor="#000000"
android:textSize="24sp"
android:layout_marginTop="10dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"/>
</LinearLayout>
对应的Activity代码,(TextViewActivity)代码:设置TextView的下划线和中划线:
package com.example.yuan.e01_textview02;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class TextViewActivity extends AppCompatActivity {
//声明引用
private TextView mTV_3;
private TextView mTV_4;
private TextView mTV_5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
//获取控件的标签用对象表示
mTV_3 = (TextView) findViewById(R.id.tv_3);
mTV_4 = (TextView) findViewById(R.id.tv_4);
mTV_5 = (TextView) findViewById(R.id.tv_5);
//设置他们的属性
mTV_3.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中划线
mTV_3.getPaint().setAntiAlias(true);//取出锯齿
mTV_4.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
mTV_5.setText(Html.fromHtml("<u>Hello yuan</u>"));//修改显示字,加下划线
}
}
运行应用程序,效果如下: