【问题标题】:How align two TextViews in one line on left side如何将两个TextViews在左侧对齐一行
【发布时间】:2017-06-08 23:34:09
【问题描述】:

我应该在一个通道中对齐两个 TextView。但是第一个 TextView 太长时应该被裁剪。

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/subtitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    tools:text="subtitle" />

                <TextView
                    android:id="@+id/date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/subtitle"
                    android:maxLines="1"
                    tools:text=" * date" />
            </RelativeLayout>

我有问题。当第一个 TextView 文本太长时,第二个 TextView 被隐藏。

First case. Short subtitle

Second case. Long subtitle. Second TextView is cropped

文本较短时,两个视图都应放在左侧。但是当文本很长时,第一个文本应该被裁剪。

【问题讨论】:

  • 我不能使用权重,因为这个视图用在RecyclerView中,性能会下降。
  • 重量也不是很好的决定,因为视图可以有很长的宽度,但是 TextViews 有不同的宽度。

标签: android layout textview


【解决方案1】:

在两个 TextView 中添加填充

android:paddingLeft="10dp"

            <TextView
                android:id="@+id/subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:paddingLeft="10dp"
                tools:text="subtitle" />

            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/subtitle"
                android:maxLines="1"
                android:paddingLeft="10dp"
                tools:text=" * date" />
        </RelativeLayout>

【讨论】:

    【解决方案2】:

    这样做可能有助于同等大小。我已经给了他们布局权重,你可以改变它并相应地调整文本视图。您可以从here 阅读更多关于线性布局权重的信息

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
    
            <TextView
                android:id="@+id/subtitle"
                style="@style/m4w_TextView.Subtitle"
                fontPath="fonts/InterstatePro-Light.otf"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:ellipsize="end"
                android:layout_weight="1"
                tools:text="subtitle" />
    
            <TextView
                android:id="@+id/date"
                style="@style/m4w_TextView.Subtitle"
                fontPath="fonts/InterstatePro-Light.otf"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:ellipsize="end"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/subtitle"
                android:maxLines="1"
                tools:text=" * date" />
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      试试下面的代码。您只需要先放置日期元素,然后在相对布局中放置字幕。

      <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
      
      <TextView
                      android:id="@+id/date"
                      android:layout_width="30dp"
                      android:layout_height="wrap_content"
                      android:maxLines="1"
                      tools:text=" * date" />
      
                  <TextView
                      android:id="@+id/subtitle"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:maxLines="1"
                      android:layout_toLeftOf="@+id/date"
                      tools:text="subtitle" />
      
      
              </RelativeLayout>
      

      【讨论】:

        【解决方案4】:

        试试这个代码,它可能对你有帮助

          <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
        
                <TextView
                    android:id="@+id/subtitle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:layout_weight="1"
                    tools:text="ajay bhimani this is practice"/>
        
                <TextView
                    android:id="@+id/date"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/subtitle"
                    android:maxLines="1"
                    android:layout_weight="1"
                    tools:text=" * date"/>
            </LinearLayout>
        

        【讨论】:

          【解决方案5】:
          <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content">
          
                      <TextView
                          android:id="@+id/subtitle"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_weight = 1
                          tools:text="subtitle" />
          
                      <TextView
                          android:id="@+id/date"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_weight = 1
                          tools:text=" * date" />
                  </LinearLayout >
          

          【讨论】:

            【解决方案6】:

            通过weight使用布局:

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            xmlns:android="http://schemas.android.com/apk/res/android">
            
            <TextView
                android:id="@+id/subtitle"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:maxLines="1"
                android:text="subtitlevvnvnvnnvnvnvnvnvnvnvnvnvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"
                android:ellipsize="end"
                android:layout_weight="0.5" />
            
            <TextView
                android:id="@+id/date"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:maxLines="1"
                android:text=" * date"
                android:layout_weight="0.5" />
            </LinearLayout>
            

            输出:

            【讨论】:

            • 当左侧textview中的文本很短时,这不会对齐左侧的两个textview
            • @BjörnKechel,您必须提供特定宽度才能使用ellipsize,使其wrap_content 与父级匹配。
            【解决方案7】:

            试试下面的代码

             <LinearLayout
                android:layout_width="match_parent"
                android:weightSum="2"
                android:orientation="horizontal"
                android:layout_height="wrap_content">
            
                <TextView
                    android:id="@+id/subtitle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:layout_weight="1"
                    android:ellipsize="end"
                    tools:text="subtitle  df  ds fgfg cvb vbcfvnb cbxfcb" />
            
                <TextView
                    android:id="@+id/date"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:maxLines="1"
                    tools:text=" * date" />
            </LinearLayout>
            

            【讨论】:

              【解决方案8】:

              尝试将RelativeLayout 替换为LinearLayout 并为两个TextView 添加layout_weight=1

              更改为:

              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
                  <TextView
                      android:id="@+id/subtitle"
                      style="@style/m4w_TextView.Subtitle"
                      fontPath="fonts/InterstatePro-Light.otf"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:maxLines="1"
                      tools:text="subtitle" />
              
                  <TextView
                      android:id="@+id/date"
                      style="@style/m4w_TextView.Subtitle"
                      fontPath="fonts/InterstatePro-Light.otf"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:layout_toRightOf="@+id/subtitle"
                      android:maxLines="1"
                      tools:text=" * date" />
              </LinearLayout>
              

              【讨论】:

                【解决方案9】:

                要实现这两者,您需要布局中的 wrap_content 第二个 TextView 中的wrap_content 和第一个 TextView 中的 width:0dp 和 layout_weight:1 的组合。这样,第二个 TextView 将始终足够大以显示内容,而第一个 TextView 将扩展到它旁边的可用空间。

                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">
                
                    <TextView
                        android:id="@+id/subtitle"
                        android:layout_width="0dp"
                        android:layout_height="40dp"
                        android:layout_weight="1"
                        android:ellipsize="end"
                        android:maxLines="1"
                        android:text="this text is tool long so it will be cut. this text is tool long so it will be cut." />
                
                    <TextView
                        android:id="@+id/date"
                        android:layout_width="wrap_content"
                        android:layout_height="40dp"
                        android:maxLines="1"
                        android:text=" * date" />
                </LinearLayout>
                

                【讨论】:

                • 非常感谢您的帮助。在我的情况下,这是最好的解决方案。
                • 太棒了,谢谢!
                猜你喜欢
                • 2012-06-19
                • 1970-01-01
                • 2014-02-14
                • 2016-08-31
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-01-04
                相关资源
                最近更新 更多