LinearLayout 中的 TextView 未包装并位于 ImageView 下
TextView in LinearLayout not wrapping and going under ImageView
有许多类似的问题,解决方案是将 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 下,没有换行。
是不是我的做法不对。有人可以帮我解决这个问题。我正在使用 AndroidStudio 3.0 开发,最低 api 级别 21(如果这很重要的话)。
提前致谢
将 RelativeLayout 更改为 LinearLayout 并将方向添加为水平。
如果还是不行,给ImageView增加weight。它应该工作。
我认为你不需要相对布局。
你应该使用这个线性布局 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 它将按您的需要工作
这将完全满足您的需要。你不需要相对布局。使用 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>
有许多类似的问题,解决方案是将 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 下,没有换行。
是不是我的做法不对。有人可以帮我解决这个问题。我正在使用 AndroidStudio 3.0 开发,最低 api 级别 21(如果这很重要的话)。 提前致谢
将 RelativeLayout 更改为 LinearLayout 并将方向添加为水平。 如果还是不行,给ImageView增加weight。它应该工作。
我认为你不需要相对布局。
你应该使用这个线性布局 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 它将按您的需要工作
这将完全满足您的需要。你不需要相对布局。使用 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>