跑马灯用到的属性和方法说明
XML中的属性 | 说明 |
---|---|
singleLine | 指定文本是否单行显示 |
ellipsize | 指定文本超出范围后的省略方式,取值说明见下表 |
focusable | 指定是否获得焦点,跑马灯设置为true |
focusableInTouchMode | 指定触摸时是否获得焦点,跑马灯设置为true |
省略方式取值说明
XML中的省略方式 | 说明 |
---|---|
start | 省略号在开头 |
middle | 省略号在中间 |
end | 省略号在末尾 |
marquee | 跑马灯显示 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="跑马灯效果,点击暂停,再点击恢复" />
<TextView
android:id="@+id/tv_marquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColor="#000000"
android:textSize="17sp"
android:text="快讯:红色预警,超强台风“莫兰蒂”即将登陆,请居民关紧门窗、备足粮草,做好防汛救灾准备!" />
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_marquee;
private boolean bPause = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_marquee = (TextView) findViewById(R.id.tv_marquee);
tv_marquee.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.tv_marquee) {
bPause = !bPause;
if (bPause) {
tv_marquee.setFocusable(false);
tv_marquee.setFocusableInTouchMode(false);
} else {
tv_marquee.setFocusable(true);
tv_marquee.setFocusableInTouchMode(true);
}
}
}
}
效果图