一、第一种方式:

  通过TextView控件的自身属性实现(但是有缺点就是当多个TextView要实现这种效果的时候,只有第一个才有效果)

   实现方法加上下面四个属性:

    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >        <TextView        android:id="@+id/textView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginTop="16dp"        android:singleLine="true"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:text="@string/textView" />        <TextView        android:layout_below="@+id/textView1"        android:id="@+id/textView2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginTop="16dp"        android:singleLine="true"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:text="@string/textView" />        </LinearLayout>

二、第二种方式:

  通过java代码实现(自定义一个TextView的类)

  加上下面四个属性:

    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >        <com.muke.textview_edittext.text        android:id="@+id/textView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginTop="16dp"        android:singleLine="true"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:text="@string/textView" />        <com.muke.textview_edittext.text        android:layout_below="@+id/textView1"        android:id="@+id/textView2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginTop="16dp"        android:singleLine="true"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:text="@string/textView" />        </LinearLayout>
package com.muke.textview_edittext;import android.content.Context;import android.util.AttributeSet;import android.widget.TextView;public class text extends TextView {    public text(Context context) {        super(context);            }        public text(Context context, AttributeSet attrs) {        super(context, attrs);            }        public text(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);            }    public text(Context context, AttributeSet attrs, int defStyleAttr,int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);            }            //关键在于这个方法    public boolean isFocused(){        return true;    }}
package com.muke.textview_edittext;import android.os.Bundle;import android.app.Activity;public class MainActivity extends Activity{        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

更多相关文章

  1. Android(安卓)动画的重复播放
  2. Android(安卓)相关属性
  3. Android开发实例大全
  4. Android(安卓)中屏幕点击事件的实现
  5. RadioGroup实现底部导航
  6. Android实现微信分享及注意事项
  7. Android实现九宫格
  8. listview常用属性记录
  9. android:configChanges属性

随机推荐

  1. Android 控件布局实现卡片效果,阴影效果
  2. android中调用App市场对自身App评分
  3. Android无法访问本地服务器(localhost)的解
  4. 给 Android 开发工程师的一份面试指南,包
  5. Android telephone
  6. android 聊天用RecyclerView获取键盘高度
  7. android离线安装
  8. Android 中ContentProvider的用法及用Con
  9. Android(安卓)WebView挂马漏洞--各大厂商
  10. Android 9.0 关机流程分析