Android выровнять TextView между двумя разделителями по горизонтали

#android #android-layout

#Android #android-макет

Вопрос:

Вот чего я пытаюсь достичь:

Вот как я пытался:

             <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/padd_loading_btn"
            android:layout_marginBottom="@dimen/padd_loading_btn" >

            <View
                android:layout_width="65dp"
                android:layout_height="1dp"
                android:layout_gravity="center_vertical"
                android:background="@android:color/darker_gray" />

            <TextView
                android:id="@ id/orLoginWithEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:text="@string/orlogin_withEmail"
                android:textSize="12sp" />

            <View
                android:layout_width="65dp"
                android:layout_height="1dp"
                android:layout_gravity="center_vertical"
                android:background="@android:color/darker_gray" />
        </LinearLayout>
  

Это выглядит нормально для моего HD-дисплея, но для других это выглядит ужасно.

Как мне сделать так, чтобы текст переносился столько, сколько ему нужно, а затем разделители равномерно выравнивались по сторонам?

p.s. Конечно, я могу установить относительный макет и внутри поместить разделитель для заполнения ширины и textview с белым фоном сверху с заполнением, но я не думаю, что это лучшее решение.

Ответ №1:

Попробуйте вот так

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/padd_loading_btn"
    android:layout_marginBottom="@dimen/padd_loading_btn" >

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@ id/orLoginWithEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center_vertical"
        android:text="@string/orlogin_withEmail"
        android:textSize="12sp" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:background="@android:color/darker_gray" />
</LinearLayout>
  

Комментарии:

1. Это именно то, чего я пытался достичь. Спасибо!

2. У меня это сработало, так что спасибо! Но я думаю, вам следует добавить: android:orientation="horizontal" к LinearLayout

Ответ №2:

Вы можете попробовать с помощью приведенного ниже кода, вы можете изменить TextViews на view.

 <?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="match_parent"
    android:orientation="horizontal" >

    <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:background="#cccccc" />

    <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.8"
                android:gravity="center_horizontal"
                android:background="#ffffff"
                android:text="Hello Android" />


    <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:background="#cccccc" />

</LinearLayout>
  

Вот результат

Изображение

Ответ №3:

Попробуйте использовать FrameLayout, не забудьте установить ширину вида сзади на «match_parent», а фон TextView — нулевым или белым, и укажите в TextView paddingLeft и paddingRight для достижения желаемого эффекта

 <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padd_loading_btn"
        android:layout_marginBottom="@dimen/padd_loading_btn" >

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_gravity="center_vertical"
            android:background="@android:color/darker_gray" />

        <TextView
            android:id="@ id/orLoginWithEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:layout_gravity="center"
            android:background="@android:color/white"
            android:text="@string/orlogin_withEmail"
            android:textSize="12sp" />

    </FrameLayout>