Android 具有3个文本视图的水平线性布局,其中第二个文本视图必须居中

Android 具有3个文本视图的水平线性布局,其中第二个文本视图必须居中,android,android-layout,Android,Android Layout,这是我第一次问自己一个问题,所以请耐心点:-)。 我有一个水平线性布局,其中有3个文本视图。 第二个字符只是一个箭头(UTF-8字符),应该精确居中。 文本视图仅为一行,如果不合适,则应简单地将其切断。 我尝试在第1个和第3个文本视图上使用权重,但如果1stTextView包含的文本太多,箭头仍会稍微向右移动,而不是保持居中(我希望权重可以确保这一点)。 以下是我迄今为止所做的尝试: <LinearLayout android:layout_width="match_parent"

这是我第一次问自己一个问题,所以请耐心点:-)。 我有一个水平线性布局,其中有3个文本视图。 第二个字符只是一个箭头(UTF-8字符),应该精确居中。 文本视图仅为一行,如果不合适,则应简单地将其切断。 我尝试在第1个和第3个文本视图上使用权重,但如果1stTextView包含的文本太多,箭头仍会稍微向右移动,而不是保持居中(我希望权重可以确保这一点)。 以下是我迄今为止所做的尝试:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1.0"
    android:orientation="horizontal">

    <TextView
        android:textSize="12sp"
        android:layout_width"wrap_content"
        android:layout_height"wrap_content"
        android:lines="1"
        android:gravity="center_vertical"
        android:layout_weight="1.0"/>

    <TextView
        android:textSize="12sp"
        android:layout_width"wrap_content"
        android:layout_height"wrap_content"
        android:lines="1"
        android:gravity="center"
        android:text="→"/>

    <TextView
        android:textSize="12sp"
        android:layout_width"wrap_content"
        android:layout_height"wrap_content"
        android:lines="1"
        android:gravity="center_vertical"
        android:layout_weight="1.0"/>

</LinearLayout>

第一个和第三个文本视图的文本在运行时以编程方式设置。 如果课文足够短,一切都好。 如果第一个文本视图中的文本太长,它会被剪切(如预期的那样!),但第二个(箭头-)文本视图仍会在一定程度上向右移动。 我希望箭头保持精确居中,并在必要时剪切左右两侧的文本。 谢谢你的帮助。

试试这个

给你的所有
TextView
width
match\u parent
而不是
wrap\u content

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center_vertical"
        android:lines="1"
        android:textSize="12sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:lines="1"
        android:text="→"
        android:textSize="12sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center_vertical"
        android:lines="1"
        android:textSize="12sp" />
</LinearLayout>

如果此方法有效,请尝试此方法

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

    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="45"
        android:gravity="center_vertical"
        android:lines="1"

        android:textSize="12sp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:lines="1"
        android:layout_weight="10"
        android:text="→"
        android:textSize="12sp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="45"
        android:gravity="center_vertical"
        android:lines="1"
        android:textSize="12sp" />

</LinearLayout>


给线性布局权重=3,并在这三个文本视图中平均分配该权重。

试试这个,它会对你有用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:weightSum="7"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:gravity="center_vertical"
        android:lines="1"

        android:textSize="12sp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:lines="3"
        android:layout_weight="1"
        android:text="→"
        android:textSize="12sp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:gravity="center_vertical"
        android:lines="1"
        android:textSize="12sp" />

</LinearLayout>

这是我将如何使用RelativeLayout来实现的:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    >
    <TextView
        android:id="@+id/txtArrow"
        android:textSize="12sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="1"
        android:gravity="center"
        android:text="?"
        android:layout_centerInParent="true"
    />
    <TextView
        android:textSize="12sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="1"
        android:gravity="center_vertical"
        android:layout_toLeftOf="@id/txtArrow"
        android:text="Left"
        android:layout_centerVertical="true"
    />
    <TextView
        android:textSize="12sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="1"
        android:gravity="center_vertical"
        android:layout_toRightOf="@id/txtArrow"
        android:text="Right"
        android:layout_centerVertical="true"
    />
</RelativeLayout>

请注意,我首先声明了中心文本视图。

然后我在其周围添加了另外两个

,但为什么首先要使用LinearLayout?这是一个典型的框架布局,箭头居中,文本视图左对齐父视图和右对齐父视图。不过,如果使用相对视图,请举例说明如何使用框架布局,这样会导致箭头占据所有空间,而第一个和第三个文本视图根本不显示。但我不知道为什么。这是我没有意识到我能做的事情:首先宣布该中心。谢谢,它很管用!这是因为这是参考视图。然后,其他两个可以通过id引用它并与之相对对齐。是的,非常感谢!我确实明白你说的;-)。我曾考虑过使用相对长度,但我真的不知道如何填充整个宽度。垂直布局是我没有意识到的。我想我大致上学会了如何使用RelativeLayouts。。