TextSwitcher 未垂直居中 layout_gravity:center_vertical

TextSwitcher not centered vertically with layout_gravity: center_vertical

我有一个垂直的 LinearLayout,里面有 2 个 TextSwitcher。有时只有第一个(@+id/ts1)会显示,有时两个都会显示在屏幕上。 ts1 的字体大小为 20,ts2 的字体大小为 16。

<LinearLayout
            android:id="@+id/linearLayout1"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:focusable="false"
            android:layout_marginLeft="@dimen/dimen_left1"
            android:visibility="gone">

            <TextSwitcher
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:id="@+id/ts1"/>

            <TextSwitcher
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:id="@+id/ts2"/>
        </LinearLayout>

我测试的时候,当它们都显示在屏幕上时,它工作正常,但是当只显示 ts1 时,文本不是垂直居中的,它更像是在顶部垂直而不是居中。我以编程方式设置了这两个 TextSwitchers.

的可见性

有人知道为什么会这样吗?

谢谢!!!

您尝试过使用 ConstraintLayout 吗?使用它可能会帮助你。如果你提供更多关于一切行为的信息,我可以给你一个代码示例。

TextSwitchers 上设置 layout_gravity 不会更改基础 TextView 的文本 android:gravity。您可以通过将 TextSwitcher 放入您的 XML 来覆盖 TextView 使用的默认值。从那里,您可以在它们上设置 android:layout_gravityandroid:gravity 以使文本垂直居中。这应该有效:

<TextSwitcher
    android:id="@+id/ts1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />
</TextSwitcher>
<TextSwitcher
    android:id="@+id/ts2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />
</TextSwitcher>

您需要为 TextView 添加引力选项,而不是 TextSwitcher。 例如,您可以动态添加 layout_width、layout_height 和重力选项。

    mSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher1);
    mSwitcher1.setFactory(new ViewSwitcher.ViewFactory() {

        public View makeView() {
            TextView myText = new TextView(MainActivity.this);
            myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
            myText.setTextSize(36);
            myText.setTextColor(Color.BLUE);

            // Add this
            myText.setGravity(Gravity.CENTER);
            // Add this
            myText.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
            return myText;
        }
    });