在回收视图中将某个字符居中

Center a certain character in recycleview

我在一个应用程序中工作,我有一个回收视图来显示用户输入的单词。

我希望它看起来像

       word1 - word2
longer word1 - word3

但我明白了

    word1 - word2
longer word1 - word3.

我试过放置三个文本视图并在回收视图中使用它们,但到目前为止没有成功。也许可以计算单词对中的字符并在末尾附加空格,以便两个单词都一样大。但这感觉像是一个糟糕的解决方案。

有什么想法吗?

LinearLayoutRelativeLayout我都试过了,当我用LinearLayout时结果如上。当我使用 RelativeLayout 时,只显示最后两个词。第一个TextView.

什么都看不到

这是 xml 文件和 java 代码

文本视图:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textSize="@dimen/active_text_size"
        android:layout_toStartOf="@id/adapter_textview2"
        android:id="@+id/adapter_textview1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:textSize="@dimen/active_text_size"
        android:id="@+id/adapter_textview2"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toEndOf="@id/adapter_textview2"
        android:textSize="@dimen/active_text_size"
        android:id="@+id/adapter_textview3"/>
</RelativeLayout>

Activity布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_root"
    tools:context="com.example.erikbylow.autoflash.TranslateActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:id="@+id/linear_input"
            android:layout_height="50dp"
            android:background="@android:color/white"
            android:orientation="horizontal">
            <EditText
                android:layout_width="0dp"
                android:layout_weight="0.75"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:id="@+id/source_input"
                android:hint="Type text to translate"/>

            <Button
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="0.25"
                android:layout_alignRight="@+id/source_input"
                android:text="Translate"
                android:clickable="true"
                android:onClick="startTranslate"
                android:background="@android:color/holo_green_dark"/>
        </LinearLayout>
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/translate_recycle"
            android:scrollbars="vertical"
            android:background="@android:color/holo_blue_bright" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

来自CustomAdapter

@Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position){
        Log.d(TAG, "Element " + position + " set." + mDataSet.get(position).w1);
        viewHolder.getLeftTextView().setText(mDataSet.get(position).w1);
        viewHolder.getCenterTextView().setText(" - ");
        viewHolder.getRightTextView().setText( mDataSet.get(position).w2);

    }

编辑: 感谢您的帮助,我是如何通过使用 ADM:s 提案并添加 textAlignment.

得到我想要的东西的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">


    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textAlignment="textEnd"
        android:textSize="@dimen/active_text_size"
        android:id="@+id/adapter_textview1"/>
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:textSize="@dimen/active_text_size"
        android:id="@+id/adapter_textview2"/>
    <TextView
        android:textAlignment="textStart"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:textSize="@dimen/active_text_size"
        android:id="@+id/adapter_textview3"/>
</LinearLayout>

从右侧对齐 TextView's

 <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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/adapter_textview2"
        android:id="@+id/adapter_textview1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_toLeftOf="@+id/adapter_textview3"
        android:id="@+id/adapter_textview2"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:id="@+id/adapter_textview3"/>
</RelativeLayout>

根据需要添加marginspadding

如果 Text 无法预测任何长度,您应该依赖 LinearLayoutlayout_weight

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/adapter_textview1"/>
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/adapter_textview2"/>
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/adapter_textview3"/>
</LinearLayout>

好的,我不知道为什么您有 3 个文本视图,而示例只连续显示 2 个。

我想到了这个 - 将布局更改为水平方向的线性布局并添加一些权重,以便第二个文本视图保持原位。

试一试,告诉我它是否有效:

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

        <TextView
            android:id="@+id/adapter_textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:padding="8dp"
            android:text="reallly loooong looooong text"
            android:textSize="@dimen/active_text_size" />

        <TextView
            android:id="@+id/adapter_textview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/adapter_textview1"
            android:layout_alignParentTop="true"
            android:layout_gravity="center"
            android:layout_weight="22"
            android:padding="8dp"
            android:text="long text"
            android:textSize="@dimen/active_text_size" />
    </LinearLayout>