TextView控件
-
android:maxLines:最大一行
-
android:ellipsize:给不显示的字体添加省略
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/bnt_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="20dp">
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱吃饼干的小熊"
android:textSize="24sp"
android:textColor="#000000" />
<TextView
android:id="@+id/tv_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="1"
android:text="爱吃饼干的小熊"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="@+id/tv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="筛选"
android:drawableRight="@drawable/icon_arrow_off"
android:textSize="24sp"
android:textColor="#000000"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱吃饼干的小熊"
android:textSize="24sp"
android:textColor="#000000"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tv_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱吃饼干的小熊"
android:textSize="24sp"
android:textColor="#000000"
android:layout_marginTop="10dp"/>
</LinearLayout>
java
package com.example.androidproject03;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mbtnTextView;//声明控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbtnTextView = findViewById(R.id.bnt_textview);//findViewById找到获取控件
mbtnTextView.setOnClickListener(new View.OnClickListener() {//setOnClickListener点击事件
@Override
public void onClick(View view) {
//跳转到TextView演示界面
Intent intent = new Intent(MainActivity.this,TextViewActivity.class);
startActivity(intent);
}
});
}
}
package com.example.androidproject03;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.TextView;
public class TextViewActivity extends AppCompatActivity {
private TextView mtv4,mtv5;
private TextView 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);//下划线
}
}