的LinearLayout体重问题对于TextViews大/小长度的文本

问题描述 投票:1回答:4

我有内部的LinearLayout 2的TextView如下:

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

      <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

       <TextView
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"/>
</LinearLayout>

上面的代码分配相等的空间来同时TextView的用省略号以文本,如果文本是大的。但在文本的长度短的情况下,同时存在的TextView之间的空间。

{LargeTextFromTextView1... LargeTextFromTextView2...}

{TextView1TextTextVeiw2Text            }

我怎样才能摆脱这种有大量的文字办案。

逻辑用于显示文本:

情况1:TextView2的文本的TextView1 +长度的文本的长度<=屏幕宽度只需显示两个的TextView的文本

情况2:在用与椭圆后半部分TextView2的椭圆和文本前半部分的TextView1 TextView2>屏幕宽度的文本的文本的TextView1 +长度的文本的长度

android android-linearlayout
4个回答
0
投票
Create like this 

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2">
    <TextView
    android:layout_width="@dimen/size_10dp"
    android:layout_height="wrap_content" 
    android:layout_weight="1"/>
    <TextView
    android:layout_width="@dimen/size_10dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>

Edit:

If textview should be single line and should end with ellipses use this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2">
<TextView
    android:layout_width="@dimen/size_10dp"
    android:layout_height="wrap_content"
    android:text="sdjhafdshfgdsfaudxzcxzcvvcvcvsfuafsdf"
    android:layout_weight="1"
    android:lines="1"
    android:ellipsize="end"/>
<TextView
    android:layout_width="@dimen/size_10dp"
    android:layout_height="wrap_content"
    android:text="ddff"
    android:layout_weight="1"
    android:lines="1"
    android:ellipsize="end"/>

0
投票

使用android:lines="1"android:ellipsize="end"不会做你期待什么。它宁愿限制的文本。例如,如果文本“删除”,你会看到“REM ......”。

为了解决这个问题,把每个TextViewRelativeLayout内。像这样:

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

  <RelativeLayout
     android:layout_width="0sp"
     android:layout_height="wrap_content"
     android:layout_weight="1">
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:text="Text"/>
  </RelativeLayout>

  <RelativeLayout
     android:layout_width="0sp"
     android:layout_height="wrap_content"
     android:layout_weight="1">
     <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:text="Text"/>
  </RelativeLayout>


0
投票

您应该使用谷歌flexboxlayout

在实施例中使用这种技术是设置flexWrap =“包装”,如上所述,

<com .google.android.flexbox.flexboxlayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:flexwrap="wrap"> 

那么你就可以得到下面的布局,其中子视图对齐到一个新行,而不是溢出其父。

还是应该使用的FlowLayout

FlowLayout

一个FlowLayout中的Android,这使得孩子的意见流向下一行的时候没有足够的空间。子视图之间的间距可以通过的FlowLayout来计算,使得视图均匀放置。

摇篮依赖性:

compile 'com.nex3z:flow-layout:1.2.4'

0
投票
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/tx1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="sdjhatfjhgjdhhvfygkjhkkkgftddrtyrtrd"
        android:lines="1"
        android:ellipsize="end"
        android:textSize="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="ddff"
        android:id="@+id/tx2"
        android:layout_toRightOf="@+id/tx1"
        android:lines="1"
        android:ellipsize="end"/>
</RelativeLayout>

您可以使用相对布局这一要求。如果改变布局是没有问题的。你所面对的是错误的,因为你是给weightSum,同样划分屏幕,因此它并不重要,如果第一个字符串的长度小,它只是分配第二个文本形式它的另一半所以乌尔让他们之间的空间。

© www.soinside.com 2019 - 2024. All rights reserved.