0

我需要布局显示:Android的布局:在TextView的右侧的TextView

  1. 如果cl__tv_team_name有简短的文字,它显示(Q)签署旁边 cl__tv_team_namecl__tv_team_name和(Q)符号彼此相邻)
  2. 如果cl__tv_team_name有长文本,textview显示它有两个或更多行,但(Q)符号是仍然可见在右侧

简单LinearLayout不起作用。 TextView cl__tv_team_name需要所有宽度。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 


    <TextView 
     android:id="@+id/cl__tv_team_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="asdfa asdfasf asdf asdf asf dsafasd fads fasdf asdfsad asdf dsaf asfd safa" /> 

    <TextView 
    android:id="@+id/cl__tv_q_sign" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="(Q)" /> 

</LinearLayout> 

我还试图RelativeLayout没有适当的效果:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical"> 

    <TextView 
     android:id="@+id/cl__tv_team_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_centerVertical="true" 
     android:text="asdfa asdfasf asdf asdf asf dsafasd fads fasdf asdfsad asdf dsaf asfd safa" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_toEndOf="@+id/cl__tv_team_name" 
     android:layout_toRightOf="@+id/cl__tv_team_name" 
     android:text="(Q)" 
     android:textSize="13sp" /> 
</RelativeLayout> 

回答

3

我会用ConstraintLayout来实现这一目标:

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="short text" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/q" 
     app:layout_constraintWidth_default="wrap" 
     app:layout_constraintHorizontal_chainStyle="packed" 
     app:layout_constraintHorizontal_bias="0"/> 

    <TextView 
     android:id="@+id/q" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="(Q)" 
     app:layout_constraintLeft_toRightOf="@+id/text" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBaseline_toBaselineOf="@+id/text"/> 

</android.support.constraint.ConstraintLayout> 

enter image description here

或做很长的第一视图中的文本:

enter image description here

+0

这就是我想要实现的。我没有时间学习约束布局,所以感谢你的代码。 – AppiDevo

+0

还有一个问题,是否可以垂直居中(Q)?所以它会被放置在第一个视图的第一行和第二行之间。 – AppiDevo

+1

是的。通过将'q'的顶部限制在'text'的顶部,并将其底部限制到'text'底部来实现。删除基线约束。 –

0

尝试相对布局内既垂直线性布局和水平线性布局的混合物。听起来令人困惑,但它通常起作用。

+0

请将工作码。 – AppiDevo

0

这应该工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical"> 

     <TextView 
      android:id="@+id/q_sign" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:text="(Q)" 
      android:textSize="13sp" /> 

     <TextView 
      android:id="@+id/cl__tv_team_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_centerVertical="true" 
      android:layout_toStartOf="@+id/q_sign" 
      android:layout_toLeftOf="@+id/q_sign" 
      android:text="asdfa asdfasf asdf asdf asf dsafasd fads fasdf asdfsad asdf dsaf asfd safa" /> 

</RelativeLayout> 
+0

我试过这个,当cl__tv_team_name包含简短文本时,(Q)符号与容器的右侧对齐,no对应于cl__tv_team_name。 – AppiDevo

+1

错过了这个要求。你尝试使用约束布局吗? –

0

尝试增加机器人:体重= 1在你的LinearLayout布局的xml文件。

<?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"> 

    <TextView 
     android:id="@+id/cl__tv_team_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="asdfa asdfasf asdf asdf asf dsafasd fads fasdf asdfsad asdf dsaf asfd safa" /> 

    <TextView 
     android:id="@+id/cl__tv_q_sign" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="(Q)" /> 
</LinearLayout> 
+0

这将无法正常工作,请检查我的要求。 – AppiDevo

+0

我的不好。我错过了这个要求。你的要求似乎有点棘手。但我认为在RelativeLayout中,如果将最大宽度分配给cl__tv_team_name textview,它可以工作。 – COD3WORM

+0

但我不知道cl__tv_team_name的最大宽度,这就是问题所在。如果我知道这一点,解决方案将非常简单;) – AppiDevo