android 自定义actionbar+字符串滚动显示

来源:互联网 发布:ping软件带地图 编辑:程序博客网 时间:2024/06/06 21:41

首先自定义TextView:

package com.android.myprojectdome.widget;import android.content.Context;import android.util.AttributeSet;import android.view.ViewDebug.ExportedProperty;import android.widget.TextView;public class ScrollTextView extends TextView {public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public ScrollTextView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public ScrollTextView(Context context) {super(context);// TODO Auto-generated constructor stub}@Override@ExportedProperty(category = "focus")public boolean isFocused() {// TODO Auto-generated method stubreturn true;}}


其次在自定义actionbar布局里引用ScrollTextView:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:id="@+id/lv_title_bg"    android:layout_height="@dimen/actionbar_height"    android:background="@android:color/black" >    <com.android.myprojectdome.widget.ScrollTextView        android:id="@+id/tv_title"        android:layout_width="150dp"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="@dimen/actionbar_title_padding"        android:background="@android:color/transparent"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:marqueeRepeatLimit="marquee_forever"        android:gravity="center"        android:singleLine="true"        android:paddingEnd="8dp"        android:textColor="@android:color/white"        android:textSize="20sp" />      <View        android:layout_width="fill_parent"        android:layout_height="1dip"        android:background="@color/line_color"        android:layout_alignParentBottom="true"/></RelativeLayout>

然后再我们要用的activity里使用setActionBarLayout:

public void setActionBarLayout(int layoutId) {ActionBar actionBar = getActionBar();if (null != actionBar) {//actionBar.setDisplayHomeAsUpEnabled(false);actionBar.setDisplayShowHomeEnabled(false);actionBar.setDisplayShowCustomEnabled(true);LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View v = inflator.inflate(layoutId, null);ActionBar.LayoutParams layout = new ActionBar.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);actionBar.setCustomView(v, layout);TextView tvTitle = (TextView) actionBar.getCustomView().findViewById(R.id.tv_title);RelativeLayout lv_title_bg = (RelativeLayout) actionBar.getCustomView().findViewById(R.id.lv_title_bg);lv_title_bg.setBackgroundColor(Color.BLACK);tvTitle.setText(R.string.action_settings);}}
再在OnCreate()里调用:

setActionBarLayout(R.layout.setting_actionbar_layout);





0 0