TextView 右侧的 RelativeLayout 视图
RelativeLayout View to right of TextView
我得到了以下简单的布局。问题可以在 android studio designer 中重现:
<?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="match_parent">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/margin_small"
android:layout_marginRight="@dimen/margin_small"
android:text="@string/long_string"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/x"
android:layout_toEndOf="@+id/x"
android:layout_centerVertical="true" />
</RelativeLayout>
如果文本视图的文本长度较短,则此布局工作正常。复选框位于文本视图的右侧。但是如果文本变长甚至可能换行,那么复选框就会被推出视图。它不再可见。我希望复选框在文本视图的右侧始终可见,即使它填满了屏幕的整个宽度。
我尝试用 LinearLayout 重写布局,但也不起作用。
<?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="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_small"
android:layout_marginRight="@dimen/margin_small"
android:text="@string/long_string"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
你知道用相对布局实现这个的技巧吗?默认情况下,我会以某种方式期望相对布局的这种行为。谢谢 ;)
您应该放置 属性 layout_weight 以使您的视图(TextView 和 Checkbox)在屏幕上具有 deff space 而不是使用硬值
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:layout_marginEnd="@dimen/margin_small"
android:layout_marginRight="@dimen/margin_small"
android:text="@string/long_string"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_weight="0.1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
一种解决方法是将文本视图和复选框置于水平方向的线性布局中。将复选框的宽度设置为您想要的任何值(常量),将文本框的宽度设置为 0dp 和 layout_weight 为 1。
这对我有用:使 checkBox alignParentRight 并将 TextView 设置为 LeftOf:
<?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="match_parent">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/y"
android:text="This is very-very-very looooooooooooong stringgggg, very-very, long-long"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
编辑。您可以将此相对布局包含到其他(父)布局中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="left">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/y"
android:text="this is veryyyyy yyyyyyyyyy yyyyyy yyy loooooo oooooooo ooon nnggggg gggg striiii iiiiin gggggg ggggg ggggggg ggg"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</RelativeLayout>
它也在工作。如果将 android:gravity="left" 放入相对布局,它会将其内容定位在左侧。
我想默认情况下无法实现所需的布局。我尝试使用 RelativeLayout、LinearLayout 和 TableLayout 来做到这一点。这些布局不支持该行为在技术上是可以理解的。相对布局必须明确地考虑到左侧或右侧的元素在父级内部最小可见的情况,即使它被放置在左侧或右侧。另一种解决方案是,如果 table 布局允许一列占用 space 的其余部分,但也尊重其他列的最小宽度。
对于我的情况,我写了一个解决方法。我使用了问题的初始相对布局,但使用以下计算为 textview 设置了最大宽度:
DisplayMetrics displaymetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int displayWidth = displaymetrics.widthPixels;
这保证了复选框可见。我知道在给定布局嵌入更复杂布局的情况下,解决方案几乎不可能。
我得到了以下简单的布局。问题可以在 android studio designer 中重现:
<?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="match_parent">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/margin_small"
android:layout_marginRight="@dimen/margin_small"
android:text="@string/long_string"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/x"
android:layout_toEndOf="@+id/x"
android:layout_centerVertical="true" />
</RelativeLayout>
如果文本视图的文本长度较短,则此布局工作正常。复选框位于文本视图的右侧。但是如果文本变长甚至可能换行,那么复选框就会被推出视图。它不再可见。我希望复选框在文本视图的右侧始终可见,即使它填满了屏幕的整个宽度。
我尝试用 LinearLayout 重写布局,但也不起作用。
<?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="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_small"
android:layout_marginRight="@dimen/margin_small"
android:text="@string/long_string"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
你知道用相对布局实现这个的技巧吗?默认情况下,我会以某种方式期望相对布局的这种行为。谢谢 ;)
您应该放置 属性 layout_weight 以使您的视图(TextView 和 Checkbox)在屏幕上具有 deff space 而不是使用硬值
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:layout_marginEnd="@dimen/margin_small"
android:layout_marginRight="@dimen/margin_small"
android:text="@string/long_string"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_weight="0.1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
一种解决方法是将文本视图和复选框置于水平方向的线性布局中。将复选框的宽度设置为您想要的任何值(常量),将文本框的宽度设置为 0dp 和 layout_weight 为 1。
这对我有用:使 checkBox alignParentRight 并将 TextView 设置为 LeftOf:
<?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="match_parent">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/y"
android:text="This is very-very-very looooooooooooong stringgggg, very-very, long-long"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
编辑。您可以将此相对布局包含到其他(父)布局中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="left">
<TextView
android:id="@+id/x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/y"
android:text="this is veryyyyy yyyyyyyyyy yyyyyy yyy loooooo oooooooo ooon nnggggg gggg striiii iiiiin gggggg ggggg ggggggg ggg"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<CheckBox
android:id="@+id/y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</RelativeLayout>
它也在工作。如果将 android:gravity="left" 放入相对布局,它会将其内容定位在左侧。
我想默认情况下无法实现所需的布局。我尝试使用 RelativeLayout、LinearLayout 和 TableLayout 来做到这一点。这些布局不支持该行为在技术上是可以理解的。相对布局必须明确地考虑到左侧或右侧的元素在父级内部最小可见的情况,即使它被放置在左侧或右侧。另一种解决方案是,如果 table 布局允许一列占用 space 的其余部分,但也尊重其他列的最小宽度。
对于我的情况,我写了一个解决方法。我使用了问题的初始相对布局,但使用以下计算为 textview 设置了最大宽度:
DisplayMetrics displaymetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int displayWidth = displaymetrics.widthPixels;
这保证了复选框可见。我知道在给定布局嵌入更复杂布局的情况下,解决方案几乎不可能。