【发布时间】:2017-02-20 19:25:44
【问题描述】:
我正在尝试将两个 TextView 并排放置,以便它们始终在屏幕上。
它们应该始终彼此相邻,接触边界。但是文字是动态变化的,所以即使第一台电视里的文字变得太长,也不应该把第二台电视推离屏幕。
这是基本布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:padding="10dp"
android:text="Aa" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:padding="10dp"
android:text="Bb" />
</LinearLayout>
这就是它的样子:
但是,如果文本变长,第二个 TextView 会被推离屏幕。
我什至使用 RelativeLayout 尝试过同样的事情。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:padding="10dp"
android:text="Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/a"
android:background="@color/colorPrimary"
android:padding="10dp"
android:text="Bb" />
</RelativeLayout>
还是同样的问题,第二个 TextView 只是不会停留在屏幕上。我希望第一个 textView 变成椭圆形,或者转换成两行而不将第二个 TextView 推离屏幕。但我也不希望第二个 TextView 卡在屏幕右侧(这可以通过给第一个 TextView 赋予 1 的权重来实现)
PS:对我来说,视图的背景并不重要,所以即使我可以通过将视图锁定在屏幕右侧来解决这个问题,也完全没问题。 为此,我尝试在 LinearLayout 中的两个视图和 RelativeLayout 中的 layout_alignParentRight 上使用 weights - 但没有任何成功。
编辑:
如果我在两种布局上都使用权重 - 这就是它的样子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:padding="10dp"
android:layout_weight="1"
android:text="Aaa" />
<TextView
android:id="@+id/b"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:padding="10dp"
android:text="Bbfbd" />
</LinearLayout>
如果文本很长,这很好,但是对于短文本,它就达不到目的了。
解决方案:
我能想到的唯一解决方案是为第一个 TextView 定义 maxWidth 属性。该属性在dimens.xml中定义,不同的屏幕尺寸有不同的值。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:ellipsize="end"
android:maxLines="1"
android:maxWidth="@dimen/max_text_view_width"
android:padding="10dp"
android:text="Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
<TextView
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:padding="10dp"
android:maxLines="1"
android:text="Bbbbbbb" />
</LinearLayout>
dimens.xml
<resources>
<dimen name="max_text_view_width">300dp</dimen>
</resources>
【问题讨论】:
-
您在使用权重时遇到了什么问题?
-
如果您的长文本包含空格怎么办?例如:aaaa aaaa aaaa aaaa
-
@JagjitSingh 如果我在第一个 TextView 上使用权重,第二个 TextView 会卡在屏幕的右边缘。我希望第一个 TextView 只占用文本所需的空间。但是,如果我对第二个 TextView 应用权重,如果第一个 TextView 中的文本太长,它仍然会被推离屏幕。
-
@Rachit 然后请尝试使用 maxWidth 和 toRightOf 属性。希望它会有所帮助
-
嘿 @Rachit 尝试使用 Google developer.android.com/reference/android/support/percent/… 的 PercentRelative 布局