2017-01-12 109 views
4

選取框工作不適合我的TextView請檢查下面的代碼的Android TextView的跑馬燈不工作

XML代碼TextView

      <TextView 
          android:id="@+id/mtextcash" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerVertical="true" 
          android:layout_marginLeft="30dp" 
          android:ellipsize="marquee" 
          android:fadingEdge="horizontal" 
          android:gravity="center" 
          android:marqueeRepeatLimit="marquee_forever" 
          android:maxLength="5" 
          android:scrollHorizontally="true" 
          android:scrollbars="horizontal" 
          android:singleLine="true" 
          android:text="Simple application that shows how to use marquee, with a long text" 
          android:textColor="@color/white" 
          android:textColorHint="@color/white" 
          android:textSize="25dp" /> 

在活動的OnCreate

TextView inputAvailableCash = (TextView) findViewById(R.id.mtextcash); 
inputAvailableCash.setSelected(true); 

謝謝預先

+0

問題在於你的android:layout_width值是wrap_content。檢查readyandroid答案。 –

+1

可能的重複http://stackoverflow.com/questions/10458844/textview-marquee-doesnt-work?rq=1或http://stackoverflow.com/questions/24165143/textview-with-marquee-is-not-工作?rq = 1 –

回答

1

在xml文件

android:focusable="true"  
android:focusableInTouchMode="true" 
0

一旦嘗試添加此屬性通過將這些PARAMS你的TextView - 它的工作原理

android:singleLine="true" 
android:ellipsize="marquee" 
android:marqueeRepeatLimit="marquee_forever" 
android:scrollHorizontally="true" 
android:focusable="true" 
android:focusableInTouchMode="true" 

而且你還需要的setSelected(真):

my_TextView.setSelected(true); 
1

對於選取框效果寬度應始終爲「match_parent」或靜態(如200dp ...等)。並編程方式使它setSelected爲true,因爲你已經完成然後只是把它的寬度作爲match_parent在XML中它將工作。

編輯的XML:

     <TextView 
         android:id="@+id/mtextcash" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_centerVertical="true" 
         android:layout_marginLeft="30dp" 
         android:ellipsize="marquee" 
         android:fadingEdge="horizontal" 
         android:gravity="center" 
         android:marqueeRepeatLimit="marquee_forever" 
         android:maxLength="5" 
         android:scrollHorizontally="true" 
         android:scrollbars="horizontal" 
         android:singleLine="true" 
         android:text="Simple application that shows how to use marquee, with a long text" 
         android:textColor="@color/white" 
         android:textColorHint="@color/white" 
         android:textSize="25dp" /> 

如果做match_parent它會在你的設計效果,那麼你必須通過固定它的寬度或用其他方式來管理它。

根據你的xml代碼,你正在使用android:maxLength =「5」意味着只有5個字符將被輸入,所以你可以修復它的寬度50dp或任何其他靜態大小。

+0

謝謝我已經嘗試過,我只是忘記刪除android:maxLength =「5」,因爲它不工作。 –

1

當你的文字大小即長度在我身邊的工作longer.its嘿檢查這個代碼,但字幕的工作。:)

<TextView 
     android:id="@+id/mywidget" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="marquee" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:singleLine="true" 
     android:textColor="#2086CA" 
     android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

希望這有助於:)

6

由於文本是非常長和和你的代碼將只工作,如果你寫在單行的文本。要麼添加

android:singleline="true" 

在xml中或將您的java代碼更改爲。

TextView inputAvailableCash = (TextView) findViewById(R.id.mtextcash); 
     inputAvailableCash.setSelected(true); 
     inputAvailableCash.setSingleLine(true); 

這一定會爲你工作。

+0

我有'maxLines =「1」',但事實證明這是不夠的。 'singleline =「true」'使它工作! 10X。 – WindRider

0
<TextView android:id="@+id/mtextcash" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="30dp" 
        android:ellipsize="marquee" 
        android:gravity="center" 
        android:marqueeRepeatLimit="marquee_forever" 
        android:scrollHorizontally="true" 
        android:singleLine="true" 
        android:text="Simple application that shows how to use marquee, with a long text" 
        android:textColor="@color/white" 
        android:textColorHint="@color/white" 
        android:textSize="25dp" />