【问题标题】:How can I activate text marquee on a TextView?如何在 TextView 上激活文本选取框?
【发布时间】:2015-08-03 23:39:56
【问题描述】:

我想在我的 RemoteView 中有一个带有选框效果的 TextView

我有这个布局 XML 描述了我的 RemoteView (R.layout.notification_loading_small) 的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="64dip"
              android:background="@color/primary_background_inverted">

    <ProgressBar android:indeterminate="true"
                 android:layout_width="64dip"
                 android:layout_height="64dip"
                 android:id="@+id/marker_progress" style="?android:attr/progressBarStyle"
                 android:layout_gravity="center_vertical|center_horizontal"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:orientation="vertical"
        android:padding="@dimen/padding_small"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/notification_small_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@color/primary_textcolor_inverted"
            android:textSize="@dimen/text_size_large"
            android:text="@string/loading"/>

        <TextView
            android:id="@+id/notification_small_textview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@color/secondary_textcolor_inverted"
            android:textSize="@dimen/text_size_small"
            android:text="@string/loading_advice"/>
    </LinearLayout>

</LinearLayout>

我的 Java 中的一个方法是这样的:

public void createTestNotification() {
        mSmallLoadingNotificationView = new RemoteViews(getPackageName(), R.layout.notification_loading_small);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                PlayerService.this)
                .setSmallIcon(R.drawable.ic_notification).setContentTitle(APP_NAME)
                .setOngoing(true).setPriority(NotificationCompat.PRIORITY_MAX)
                .setContent(mSmallLoadingNotificationView);

        mNotification = builder.build();

        startForeground(PLAYERSERVICE_NOTIFICATION_ID, mNotification);
}

我想让 id/notification_small_textview2 中的文本水平选框,但到目前为止这似乎是一项不可能完成的任务......

根据网上我需要添加 android:ellipsize="marquee", android:marqueeRepeatLimit="marquee_forever", android:lines="1 "android:singleLine="true" 在我的 TextView 中。似乎我们还需要从 Java 中的 TextView 调用方法 setSelected(true)。但是我找不到用那种方法来做到这一点......我错过了什么?

我应该如何修改我的 Java 代码才能使选取框正常工作?

【问题讨论】:

    标签: android performance android-layout android-fragments remoteview


    【解决方案1】:

    我认为只有当文本显示长度大于TextView 的宽度时,选框方式才会起作用,并且由于您将其设置为match_parent,因此它需要非常长文本。

    另外,您需要设置可聚焦属性。 我尝试了以下方法:

    <TextView
        android:text="Some string here that is wider than your screen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"/>
    

    如果你的字符串不够长,首先想到的是为TextView 左右设置动画。或者也许实现您的自定义TextView,它使用偏移量绘制文本。

    【讨论】:

    【解决方案2】:

    我不确定您可以使用通知做什么,但如果问题与 TextView 有关,您可以使用 WebView 来代替一些 HTML。

    https://stackoverflow.com/a/17007724/3545278

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-08
      • 2013-06-28
      • 1970-01-01
      • 2011-08-22
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      相关资源
      最近更新 更多