首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在TextSwitcher中对齐文本?

如何在TextSwitcher中对齐文本?
EN

Stack Overflow用户
提问于 2018-06-08 09:02:39
回答 2查看 350关注 0票数 0

我想用安卓来证明我的TextSwitcher是合理的。但我找不到任何解决办法。我增加了一个TextSwitcher,如下所示

代码语言:javascript
复制
 <TextSwitcher
    android:id="@+id/ts_place"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginStart="@dimen/left_offset"/>

但文字没有左对齐和右对齐,如下图-

斯塔克斯统治了北方,直到罗布·斯塔克在罗斯·博尔顿的红色婚礼上背叛了他。从那以后,是博尔顿统治了北方,他们的座位从德雷德堡转移到了临冬城。卡斯塔克、曼德利、翁伯、里德和莫蒙特是宣誓效忠北方典狱长的主要附庸。

我想要使这些文字左对右对齐。

EN

回答 2

Stack Overflow用户

发布于 2018-06-08 13:42:57

创建一个ViewFactory并重写makeView函数,以便为文本切换程序使用您自己的自定义视图/文本视图。您应该能够在该视图上设置属性。

代码语言:javascript
复制
TextSwitcher exampleTextSwitcher;
exampleTextSwitcher.setFactory(textSwitcherFactory);


private ViewSwitcher.ViewFactory textSwitcherFactory = new ViewSwitcher.ViewFactory()
{
    @Override
    public View makeView()
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        return inflater.inflate(R.layout.view_timer_textview, null);
    }
};

view_timer_textview.xml

代码语言:javascript
复制
<TextView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/test"
    android:includeFontPadding="true"
    android:textAlignment="center"
    android:layout_gravity="center"
    android:lines="1"
    android:ellipsize="end"/>
票数 0
EN

Stack Overflow用户

发布于 2020-05-13 12:36:25

您还可以通过代码直接完成此操作,例如:

代码语言:javascript
复制
TextSwitcher textSwitcher = findViewById(R.id.your_id_text_switcher);

        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            tvSwitcher = new TextView(MainActivity.this);
            tvSwitcher.setTextColor(getResources().getColor(R.color.textColor));
            tvSwitcher.setTextSize(18);
            //edit lines as follows
            tvSwitcher.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
            return tvSwitcher;
        }
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50757017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档