ljx646566715
一:如何自定义TextView实现滚动效果
  1. 继承TextView基类
  2. 重写构造方法
  3. 修改isFocused()方法,获取焦点。
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class FocusedTextView extends TextView {

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

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

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

    public boolean isFocused() {
        return true;
    }

}

二:布局xml文件写法

主要设置如下两项:
android:singleLine="true" 单行显示
android:ellipsize="marquee"滚动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:orientation="vertical" >

    <!--FocusedTextView的全路径名称,其他选项设置和TextView一样-->
    <com.android.settings.utils.FocusedTextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:includeFontPadding="false"
        android:paddingLeft="@dimen/item_padding_x"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:textColor="@drawable/app_text_color"
        android:textSize="@dimen/item_textsize" />

    <com.android.settings.utils.FocusedTextView
        android:id="@+id/summary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:includeFontPadding="false"
        android:paddingLeft="@dimen/item_padding_x"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:text="Summary"
        android:textColor="@drawable/app_text_color"
        android:textSize="@dimen/item_textsize" />
</LinearLayout>
三:总结
  1. 继承基类TextView
  2. 修改isFocused()方法
  3. 设置TextView属性

喜欢源码分析系列可参考其他文章:
Android源码分析(一)-----如何快速掌握Android编译文件
Android源码分析(二)-----如何编译修改后的framework资源文件
Android源码分析(三)-----系统框架设计思想
Android源码分析(四)-----Android源码编译及刷机步骤
Android源码分析(五)-----如何从架构师的角度去设计Framework框架

分类:

技术点:

相关文章: