0

请看下面的代码

<TableRow
          android:id="@+id/tableRow13"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="5dp" >

            <TextView
                 android:id="@+id/textView22"
                  android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:textColor="#ffffff"
                 android:textSize="12sp"
                 android:text="@string/r_new_event"
                 android:layout_marginLeft="5dp"
                 android:textAppearance="?android:attr/textAppearanceSmall" />

             <TextView
                 android:id="@+id/textView23"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Meeting with people i have never seen before in my entire life lol"
                 android:ellipsize="marquee"
                 android:maxLength="10"
                 android:singleLine="true"
                 android:fadingEdge="horizontal"
                 android:marqueeRepeatLimit="marquee_forever"
                 android:scrollHorizontally="true"
                 android:textColor="#ffffff"
                 android:textSize="12sp"
                 android:layout_marginLeft="10dp"
                 android:textAppearance="?android:attr/textAppearanceSmall" />

</TableRow>

如果文本长度超过 10 个字母,则此尝试是选中文本。但不幸的是,这段代码不起作用。我试过飞蛾模拟器和安卓手机。这里有什么问题?

更新

以下是我的新代码,它仍然无法正常工作

<TextView
    android:id="@+id/textView23"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Meeting with people i have never seen before in my entire life lol"
    android:ellipsize="marquee"
    android:maxLength="10"
    android:singleLine="true"
    android:fadingEdge="horizontal"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:textColor="#ffffff"
    android:textSize="12sp"
    android:layout_marginLeft="10dp"
    android:focusable="true" 
android:focusableInTouchMode="true" 
    android:freezesText="true"
    android:textAppearance="?android:attr/textAppearanceSmall" />
4

4 回答 4

3

试试这个代码..

XML

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/mywidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:lines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ff4500"
        android:text="Simple application that shows how to use marquee, with a long text" />
</RelativeLayout>

主要活动

public class TextViewMarquee extends Activity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) this.findViewById(R.id.tv);  
        tv.setSelected(true);  // Set focus to the textview
    }
}
于 2013-10-29T12:47:55.327 回答
0

添加这些:

android:focusable="true" 
android:focusableInTouchMode="true" 
android:freezesText="true"
于 2013-10-29T12:46:05.240 回答
0

用下面的代码替换你的 textView

<TextView
                 android:id="@+id/textView23"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Meeting with people i have never seen before in my entire life lol"
                 android:ellipsize="marquee"
                   android:lines="1"
                 android:singleLine="true"
                 android:fadingEdge="horizontal"
                 android:marqueeRepeatLimit="marquee_forever"
                 android:scrollHorizontally="true"
                 android:textColor="#ffffff"
                 android:textSize="12sp"
                 android:layout_marginLeft="10dp"
                 android:focusable="true" 
                 android:focusableInTouchMode="true" 
                 android:freezesText="true"
                 android:textAppearance="?android:attr/textAppearanceSmall" />
于 2013-10-29T12:53:10.180 回答
0

使用下面的自定义 TextView / onCreate 只需写 your_textView.setSelected(true);

public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyTextView(Context context) {
    super(context);
}

@Override
protected void onFocusChanged(boolean focused, int direction,
        Rect previouslyFocusedRect) {
    if (focused)
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
}

@Override
public void onWindowFocusChanged(boolean focused) {
    if (focused)
        super.onWindowFocusChanged(focused);
}

@Override
public boolean isFocused() {
    return true;
}

}

于 2013-10-29T13:11:38.350 回答