【问题标题】:Linear Layout with orientation horizontal makes the text invisible when the length increases水平方向的线性布局使文本在长度增加时不可见
【发布时间】:2017-09-11 10:34:28
【问题描述】:

当第一个文本的长度增加时,具有水平方向的 Android 线性布局使下一个 TextViews 不可见。如果第一个 textview 长度超过一行,则剩余的 textviews 将不可见。我只想环绕文本,即,如果第一个文本视图的长度增加,则其余视图应位于第一个视图之下。

我可以在不以编程方式更改方向的情况下实现吗?

我的代码如下:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <com.mikhaellopez.circularimageview.CircularImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:scaleType="centerCrop"
        app:civ_border_color="@color/colorAccent"
        app:civ_border_width="5dp"
        app:civ_border="true"
        android:src="@drawable/user"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="80dp"
        android:layout_marginStart="80dp"
        android:layout_marginEnd="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:maxLines="2"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:ellipsize="end"
            android:layout_margin="3dp"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="3dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:ellipsize="end"
                android:maxLines="1"
                android:textSize="14sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/period"
                android:textSize="16sp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginEnd="8dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/views"
                android:textSize="14sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/period"
                android:textSize="16sp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginEnd="8dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hoursago"
                android:textSize="14sp"/>
        </LinearLayout>
    </LinearLayout>
    <ImageButton
        android:id="@+id/popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|center"
        android:background="@null"
        android:clickable="true"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_centerInParent="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/ic_overflow_grey_24dp" />
</RelativeLayout>

我得到的屏幕是

【问题讨论】:

  • 是的,你可以使用Horizo​​ntalScrollView来实现下一个textview,或者你可以固定第一个TextView的宽度。
  • 你可以尝试给每个 texview 赋予权重
  • 虽然我给出了权重,但文本在指定的权重内对齐。我只想环绕文本,即,如果第一个 textview 的长度增加,则其余视图应位于第一个视图下

标签: android textview android-linearlayout


【解决方案1】:

尝试将以下内容添加到您的线性布局中

android:weightSum="1"

然后在每个 TextView 中添加允许存在的数量。

android:layout_weight="your value here"

举个例子

android:layout_weight="0.2"

现在允许填充 20% 的线性布局。

【讨论】:

    【解决方案2】:

    也许你可以通过分配 LinearLayout 权重来归档它,试试下面的代码

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <com.mikhaellopez.circularimageview.CircularImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:scaleType="centerCrop"
        app:civ_border_color="@color/colorAccent"
        app:civ_border_width="5dp"
        app:civ_border="true"
        android:src="@drawable/user"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="80dp"
        android:layout_marginStart="80dp"
        android:layout_marginEnd="35dp"
        android:layout_marginRight="35dp"
        android:layout_marginTop="15dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:maxLines="2"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:ellipsize="end"
            android:layout_margin="3dp"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_margin="3dp"
            android:weightSum="8">
            <TextView
                android:layout_weight="2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="StramTube"
                android:ellipsize="end"
                android:maxLines="1"
                android:textSize="14sp"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="*"
                android:textSize="16sp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginEnd="8dp" 
                android:layout_weight="1"/>
            <TextView
                android:layout_weight="2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="view"
                android:textSize="14sp"/>
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="*"
                android:textSize="16sp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginEnd="8dp"
                android:layout_weight="1"/>
            <TextView
                android:layout_weight="2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Hours ago"
                android:textSize="14sp"/>
        </LinearLayout>
    </LinearLayout>
    <ImageButton
        android:id="@+id/popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|center"
        android:background="@null"
        android:clickable="true"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:layout_centerInParent="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/ic_overflow_grey_24dp" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多