【问题标题】:Too long TextView makes other Views disappear太长的TextView使其他View消失
【发布时间】:2016-10-17 17:23:26
【问题描述】:

当 TextView 中的文本太长时,如何阻止复选框在下面的代码中消失?我对为 TextView 硬编码 max_width 不感兴趣,我想显示我的整个文本。

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

            <TextView
                android:id="@+id/tv"
                style="@style/Text"
                android:layout_weight="0"
                android:text="@string/multiple_sounds" />

            <CheckBox
                android:id="@+id/cb"
                style="@style/Text"
                android:layout_gravity="right"
                android:layout_weight="1"/>

        </LinearLayout>

styles.xml

<style name="Text">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:padding">@dimen/margin</item>
    <item name="android:textSize">@dimen/text</item>
    <item name="android:textAlignment">center</item>
</style>

【问题讨论】:

    标签: android android-layout user-interface textview android-linearlayout


    【解决方案1】:

    我会使用重量。将 android:weightSum 添加到您的 LinearLayout 并带有值 1
    LinearLayout 中的每个元素添加权重。例如,0.8 用于 textview,0.2 用于 Checkbox

    然后将每个元素的宽度设置为0dp

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">
    
            <TextView
                android:id="@+id/tv"
                style="@style/Text"
                android:layout_weight="0.8"
                android:text="@string/multiple_sounds" />
    
            <CheckBox
                android:id="@+id/cb"
                style="@style/Text"
                android:layout_gravity="right"
                android:layout_weight="0.2"/>
    
        </LinearLayout>
    

    更新你的风格:

        <style name="Text">
            <item name="android:layout_height">wrap_content</item>
            <item name="android:layout_width">0dp</item>
            <item name="android:padding">@dimen/margin</item>
            <item name="android:textSize">@dimen/text</item>
            <item name="android:textAlignment">center</item>
        </style>
    

    【讨论】:

    • +1 你读懂了我的想法,而我正在写一个答案:)。关键是 &lt;item name="android:layout_width"&gt;0dp&lt;/item&gt; 这就是问题所在,当然没有理由在 TextView 上使用 android:layout_weight="0"
    【解决方案2】:

    如果您要在 textview 上显示大数据,我建议您在父布局中使用滚动视图。

    【讨论】:

      【解决方案3】:

      我迟到了,但我希望能帮助 smn 解决这个问题。有同样的问题checkBox使LinearLayout无法访问TextView,checkBox和textView都需要在xml中有这一行:

      android:layout_gravity="center_vertical"
      

      【讨论】:

        【解决方案4】:

        使用文本视图权重 = 1 和其他视图作为 wrap_content,因此其他视图的大小不会根据您的文本而改变

         <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">
        
            <TextView
                android:id="@+id/tv"
                style="@style/Text"
                android:layout_weight=""
                android:text="@string/multiple_sounds" />
        
            <CheckBox
                android:id="@+id/cb"
                style="@style/Text"
                android:layout_gravity="right"
                android:layout_weight="wrap_content"/>
        
        </LinearLayout>
        

        【讨论】:

          【解决方案5】:

          如果你想让文本换行(使用RelativeLayout):

          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
          
          <CheckBox
              android:id="@+id/checkBox"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true" />
          
          <TextView
              android:id="@+id/textView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_toStartOf="@id/checkbox" />
          
          </RelativeLayout> 
          

          另一个解决方案是使用这个库来缩小文本: https://github.com/grantland/android-autofittextview

          【讨论】:

            【解决方案6】:

            我建议使用 RelativeLayout 来保持 UI 的一致性。下面是示例相对布局代码

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2">
            
            <TextView
                android:id="@+id/tv"
                style="@style/Text"
                android:layout_toLeftOf="@+id/cb"
                android:text="@string/multiple_sounds" />
            
            <CheckBox
                android:id="@+id/cb"
                style="@style/Text"
                android:layout_gravity="right"
                android:layout_alignParentEnd="true"/>
            
            </RelativeLayout>
            

            【讨论】:

              猜你喜欢
              • 2018-08-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-04-30
              • 2012-05-16
              • 1970-01-01
              相关资源
              最近更新 更多