【问题标题】:TextView in LinearLayout not wrapping and going under ImageViewLinearLayout 中的 TextView 不包装并在 ImageView 下
【发布时间】:2017-11-11 21:06:52
【问题描述】:

解决方案将 layout_weight="1" 添加到文本视图中有许多类似的问题。我试过了,但它没有帮助我。我的情况不同。不要认为它是重复的。

下面是我想做的布局。如果图像视图中有图像,则 TextView 应该换行。如果没有图像,应该是原样。

下面是我目前尝试的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="2dp">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_alignParentStart="true"
        android:gravity="start|center_vertical"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="some text here"/>
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="some text here which should wrap depending upon
            the string length and also considering whether or not there is an image adjacent to it"/>
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="some text here"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="10dp"
        android:adjustViewBounds="true"
        android:src="@drawable/save"/>
</RelativeLayout>
</android.support.v7.widget.CardView>

下面是这段代码的结果。 TextView 在 ImageView 下不换行。

我的方法错了吗?有人可以帮我解决这个问题。我正在使用最低 api 级别 21 开发的 AndroidStudio 3.0(如果这很重要)。 提前致谢

【问题讨论】:

    标签: android android-layout android-linearlayout android-cardview


    【解决方案1】:

    这将完全满足您的需求。您不需要相对布局。将线性布局更改为水平的这种相对布局。然后将垂直线性布局宽度更改为 0dp 并添加权重:1

    XML 代码:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView 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="wrap_content"
        android:layout_margin="5dp"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="2dp">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="some text here"
                    android:layout_weight="1"/>
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="some text here which should wrap depending upon
                the string length and also considering whether or not there is an image adjacent to it"/>
                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="some text here"/>
            </LinearLayout>
    
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:adjustViewBounds="true"
                android:src="@mipmap/ic_launcher"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>
    

    【讨论】:

    • 感谢您的快速回复。这是工作。但是你能解释一下这件事吗?为什么会发生这种情况以及 layout_weight 的魔力
    • 水平线性布局有两个孩子,一个垂直线性布局和另一个图像。我们将垂直线性布局设置为 weight: 1 这意味着它将一直占用 100% 的空间,因为 imageview 没有权重。但是当图像可用时,它将占用除图像空间外的 100% 空间,当图像不可用时,它将占用其父级的 100% 空间而没有图像。
    【解决方案2】:

    我认为您不需要相对布局。

    你应该使用这种线性布局 5 次。

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            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="wrap_content"
            android:layout_gravity="start"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="some text here which should wrap depending upon
            the string length and also considering whether or not there is an image adjacent to it"
                android:layout_weight="1"
                />
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_launcher_background"
                />
        </LinearLayout> 
    

    如果没有图片 textview 它将按照您的意愿工作

    【讨论】:

      【解决方案3】:

      将 RelativeLayout 更改为 LinearLayout 并将方向添加为水平。 如果还是不行,给 ImageView 添加权重。它应该工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-15
        • 1970-01-01
        • 2015-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-01
        相关资源
        最近更新 更多