Android LinearLayout中的文本视图未在ImageView下换行
对于许多类似的问题,解决方案是在文本视图中添加layout_weight=“1”。我试过了,但没用。我的情况不同。不要认为它是重复的。 下面是我想做的布局。如果图像视图中有图像,则TextView应换行。如果没有图像,则应保持原样 下面是我到目前为止尝试的代码Android LinearLayout中的文本视图未在ImageView下换行,android,android-layout,android-linearlayout,android-cardview,Android,Android Layout,Android Linearlayout,Android Cardview,对于许多类似的问题,解决方案是在文本视图中添加layout_weight=“1”。我试过了,但没用。我的情况不同。不要认为它是重复的。 下面是我想做的布局。如果图像视图中有图像,则TextView应换行。如果没有图像,则应保持原样 下面是我到目前为止尝试的代码 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.andr
<?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>
我的方法错了吗。有人能帮我解决这个问题吗。我正在使用AndroidStudio 3.0开发最小api级别为21(如果有任何问题的话)。
提前感谢将RelativeLayout更改为LinearLayout,并将方向添加为水平方向。
如果仍不起作用,请向ImageView添加权重。它应该会起作用 我认为您不需要相对布局
您应该使用此线性布局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>
如果没有图片,文本视图将根据您的需要工作
这正是您所需要的。您不需要相对布局。更改此相对布局,将linearlayout设置为水平。然后更改垂直线布局宽度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>
感谢您的快速回复。它正在工作。但是你能解释一下吗。为什么会发生这种情况以及布局的魔力水平线性布局有两个孩子,一个是垂直线性布局,另一个是图像。我们将垂直线性布局设置为权重:1,这意味着它将始终占用100%的空间,因为imageview没有权重。但当图像可用时,除图像空间外,它将占用100%的空间;当图像不可用时,它将占用其父对象的100%空间,而不包含图像。