Android TextView 의 주마등 효과 구현 방법

3847 단어 android주마등
본 고 는 매우 간단 하면 서도 자주 사용 되 는 컨트롤,주마등 상태의 TextView 를 공유 하고 자 한다.표시 할 텍스트 의 길이 가 너무 길 고 줄 을 바 꾸 고 싶 지 않 을 때 텍스트 를 표시 합 니 다.첫째,텍스트 를 완전히 표시 할 수 있 습 니 다.둘째,효과 도 멋 지고 실현 하기 가 매우 간단 합 니 다.그래서 기꺼이 하지 않 겠 습 니까?먼저 효과 그림 보기:


코드 구현
TextView 는 경마등 기능 을 가지 고 있 으 며,ellipsize 속성 을 marquee 로 설정 하면 됩 니 다.단,TextView 가 선 택 된 상태 에 있어 야 효과 가 있다 는 전제 가 있 습 니 다.이 를 보면 자 연 스 럽 게 컨트롤 을 사용자 정의 하여 다음 코드 를 작성 합 니 다.

public class MarqueeTextView extends TextView {

 public MarqueeTextView(Context con) {
 super(con);
 }

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

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

 @Override
 public boolean isFocused() {
 // TODO Auto-generated method stub
 if(getEditableText().equals(TruncateAt.MARQUEE)){
  return true;
 }
 return super.isFocused();
 }
}
isFocused 방법 을 다시 쓰 고 판단 합 니 다.marqueen 속성 을 설정 해 야 선택 상 태 를 유지 합 니 다.그렇지 않 으 면 일반 TextView 와 같 습 니 다.다음은 바로 사용 할 수 있 습 니 다.레이아웃 을 보 세 요.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <FrameLayout
 android:id="@+id/titlebar_layout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#39ac69" >
 <LinearLayout

  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="#ffffff"
  android:gravity="center_vertical"
  android:orientation="horizontal" >

  <ImageView
  android:id="@+id/home_location_iv"
  android:layout_width="25dp"
  android:layout_height="27dp"
  android:layout_marginLeft="10dp"
  android:scaleType="fitXY"
  android:src="@drawable/icon_place" />

  <com.lxj.marqueetextview.MarqueeTextView
  android:id="@+id/home_location_tv"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_marginLeft="10dp"
  android:layout_marginRight="10dp"
  android:layout_weight="1"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:gravity="center"
  android:marqueeRepeatLimit="marquee_forever"
  android:scrollHorizontally="true"
  android:singleLine="true"
  android:text="    ..."
  android:textColor="#39ac69"
  android:textSize="18sp" />

  <ImageView
  android:id="@+id/home_search_iv"
  android:layout_width="25dp"
  android:layout_height="27dp"
  android:layout_marginRight="10dp"
  android:scaleType="fitXY"
  android:src="@drawable/icon_place" />
 </LinearLayout>
</FrameLayout>
</LinearLayout>
두 개의 ellipsize 속성 을'marquee'로 설정 해 야 합 니 다.줄 의 속성 인 singleLine 은 true 로 설정 해 야 합 니 다.이로써 TextView 의 주마등 효과 가 이 루어 졌 다.
본 고 는 여러분 들 이 안 드 로 이 드 소프트웨어 프로 그래 밍 을 배 우 는 데 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기