折叠工具栏布局android标题椭圆形

时间:2016-12-12 08:17:29

标签: android android-collapsingtoolbarlayout ellipsis androiddesignsupport

我正在使用Android设计支持库25.0.1,在折叠工具栏布局中标题有一个问题,我的标题很长,即使有空间显示,它也会在展开状态下进行椭圆化处理。 / p>

是否有任何解决方案可以在扩展模式下使用ellipsize显示标题。

although there is some space availbale the title is ellispsized

折叠工具栏代码:

<net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/white"
            app:titleEnabled="true"
            app:expandedTitleGravity="center_horizontal"
            app:expandedTitleMarginTop="75dp"
            app:expandedTitleTextAppearance="@style/ExpandedAppBar"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

2 个答案:

答案 0 :(得分:0)

您可以创建自定义的Marquee-able工具栏,此代码可以帮助您

public class MarqueeToolbar extends Toolbar {

    TextView title;

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

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

    public MarqueeToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setTitle(CharSequence title) {
        if (!reflected) {
            reflected = reflectTitle();
        }
        super.setTitle(title);
        selectTitle();
    }

    @Override
    public void setTitle(int resId) {
        if (!reflected) {
            reflected = reflectTitle();
        }
        super.setTitle(resId);
        selectTitle();
    }

    boolean reflected = false;
    private boolean reflectTitle() {
        try {
            Field field = Toolbar.class.getDeclaredField("mTitleTextView");
            field.setAccessible(true);
            title = (TextView) field.get(this);
            title.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            title.setMarqueeRepeatLimit(-1);
            return true;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            return false;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        } catch (NullPointerException e) {
            e.printStackTrace();
            return false;
        }
    }

    public void selectTitle() {
        if (title != null)
            title.setSelected(true);
    }
}

答案 1 :(得分:0)

我正在使用https://github.com/opacapp/multiline-collapsingtoolbar这个库来解决我的问题,除了标题中的阴影外,它的效果很好

相关问题