创建TextView
1.创建一个新的Active(置于与MainActive同一目录下)
2.在MainActivity中创建Button,通过Button跳转到TextViewActivity
MainActive
public class MainActivity extends Activity {
private Button mBtnTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnTextView = (Button) findViewById(R.id.btn_textview);
mBtnTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//跳转到演示界面
Intent intent = new Intent(MainActivity.this, TextViewActivity.class);
startActivity(intent);
}
});
}
}
3.在TextViewActive下查找代码,并访问"activity_text_view.xml"文件
setContentView(R.layout.activity_text_view);
4.在xml文件下即可编辑界面
activity_text_view.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"
android:background="#669999">
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_test1"
android:textColor="#000000"
android:textSize="36sp" />
<TextView
android:id="@+id/tv_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:text="helloworld"
android:textColor="#000000"
android:textSize="36sp"
android:layout_marginTop="15dp"/>
<TextView
android:id="@+id/tv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="筛选"
android:drawableRight="@drawable/arrowdown"
android:drawablePadding="10dp"
android:textColor="#000000"
android:textSize="36sp"
android:layout_marginTop="15dp"/>
<TextView
android:id="@+id/tv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="中划线"
android:textColor="#000000"
android:textSize="36sp" />
<TextView
android:id="@+id/tv_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="下划线"
android:textColor="#000000"
android:textSize="36sp" />
<TextView
android:id="@+id/tv_6"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColor="#000000"
android:textSize="36sp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:text="AndroidAndroidAndroidAndroid"/>
</LinearLayout>
- 为实现中划线,下划线,跑马灯效果,在TextViewActive中更新代码
TextViewActive
package com.test.semir.firstapp;
import android.app.Activity;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class TextViewActivity extends Activity {
private TextView mTv4,mTv5,mTv6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
mTv4 = findViewById(R.id.tv_4);
mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
mTv4.getPaint().setAntiAlias(true); //消除锯齿
mTv5 = findViewById(R.id.tv_5);
mTv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
mTv5.getPaint().setAntiAlias(true);
mTv6 = findViewById(R.id.tv_6);
mTv6.setSingleLine(true);//设置单行显示
mTv6.setHorizontallyScrolling(true);//横向滚动
mTv6.setMarqueeRepeatLimit(-1);
mTv6.setSelected(true);//开始滚
}
}