Textview setText左右

时间:2017-04-12 10:10:15

标签: android android-layout textview android-xml android-relativelayout

这是我的Textview

  <TextView
            android:id="@+id/vagon_class_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="II Class 19 EUR"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:textSize="12dp" />

正如你所看到的那样,文字是II类19 Eur。当我左侧只有一个Textview / II类,右侧只有19 Eur时,我可以写这样的文本。 我尝试得到这个解决方案,因为我有textSelector xml,当我有两个textview不正常时。只有第一个textview的选择器工作。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#ffffff"/>
<item android:state_enabled="false" android:color="#4d4d4d" />
<item android:color="#4d4d4d"/>

    <LinearLayout
        android:id="@+id/selected_layout"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="60dp"
        android:clickable="true"
        android:focusable="true"
        android:background="@drawable/recyclerview_selected_departure"
        android:focusableInTouchMode="true">

        <TextView
            android:id="@+id/vagon_class_name"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:paddingLeft="16dp"
            android:text="II Class"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:textColor="@drawable/testselectorcolor"
            android:gravity="center|left"
            android:textSize="12dp" />


        <TextView
            android:id="@+id/u_major_text"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center|right"
            android:singleLine="true"
            android:text="25 EUR"
            android:clickable="true"
            android:focusable="true"

            android:focusableInTouchMode="true"
            android:textColor="@drawable/testselectorcolor"
            android:paddingRight="16dp"
            android:textSize="18dp"


            />


    </LinearLayout>

I want to receive like this result

3 个答案:

答案 0 :(得分:0)

现在,您可以同时对背景和文本颜色状态更改进行操作

<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:background="@drawable/bg_selector"
android:descendantFocusability="afterDescendants"
android:orientation="horizontal">

<TextView
  android:id="@+id/vagon_class_name"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:paddingLeft="16dp"
  android:gravity="center"
  android:text="II Class"
  android:textColor="@drawable/text_color_selector"
  android:textSize="16sp" />

<android.support.v4.widget.Space
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="1" />

<TextView
  android:id="@+id/u_major_text"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:paddingRight="16dp"
  android:gravity="center"
  android:maxLines="1"
  android:text="25 EUR"
  android:textColor="@drawable/text_color_selector"
  android:textSize="16sp" />

文本选择器:drawable / text_color_selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/colorAccent" android:state_pressed="true" />
  <item android:color="@color/colorPrimary" android:state_pressed="false" android:state_selected="false" />
  <item android:color="@color/colorPrimary" android:state_pressed="false" android:state_selected="true" />
</selector>

后台选择器:drawable / bg_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@color/selector_pressed" android:state_pressed="true" /> <!-- pressed -->
  <item android:drawable="@color/selector_focussed" android:state_focused="true" /> <!-- focused -->
  <item android:drawable="@android:color/transparent" /> <!-- default -->
</selector> 

答案 1 :(得分:0)

<LinearLayout
  android:orientation="horizontal"
  ...>

  <TextView
     android:width = "match_parent"
     android:weight ="1" />

  <TextView
     android:width = "match_parent"
     android:weight ="1"
     android:gravity = "end|right" />

</LinearLayout>

答案 2 :(得分:0)

尝试以下代码进行左右对齐

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft = "10dp"
    android:layout_marginRight = "10dp"
    android:layout_marginTop = "10dp"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/alreadymem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_weight="1"
        android:text="Already a Member?"
        />
    <TextView
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:textColor="@color/textLoginAccount"
        android:layout_weight="1"
        android:text="Login"
        />
</LinearLayout>