当 RelativeLayout 中右侧有一个 ImageView 时,如何使按钮的宽度与父级匹配?
How to make a button's width match parent while there is an ImageView on its right in a RelativeLayout?
这就是现在的样子。
使用 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:padding="16dp">
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/crime_title_label" />
<EditText
android:id="@+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:hint="@string/crime_title_hint" />
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/crime_title"
android:text="@string/crime_details_label" />
<Button
android:id="@+id/crime_date"
android:layout_below="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_below="@+id/crime_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_solved_label" />
<Button
android:id="@+id/suspect_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:text="@string/choose_suspect" />
<Button
android:id="@+id/send_crime_report"
android:text="@string/send_crime_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/suspect_button" />
<ImageView
android:id="@+id/callImage"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:layout_alignBottom="@+id/suspect_button"
android:layout_toRightOf="@id/suspect_button"
app:srcCompat="@android:drawable/sym_action_call" />
</RelativeLayout>
我想要做的是,让 ImageView callImage
位于 suspect_button
的右侧,并且整体位于最右侧。为此,我希望尽可能延长 suspect_button
,就像您在 match_parent
中所做的那样。但是,当我执行 match_parent
时,ImageView 会丢失。简而言之,我想“match_parent
”suspect_button
,但我的 ImageView callImage
位于 suspect_button
的右侧,同时不会在外观上迷路。
如果您不建议需要我使用其他布局的解决方案,我将不胜感激。我想了解是否可能,或者如何在相对布局中做我想做的事情。
谢谢。
您可以在 suspect_button 中使用 layout_toLeftOf 并传入 callImage 的 ID。这样你的按钮将占据屏幕的宽度,但将为图像留出空间。
嘿伙计,你必须尝试 LinearLayout 并在两个元素中设置它的 android:layout_weight="5"!这将平均分配这两个元素!
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/send_crime_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="sdfsdfsd" />
<ImageView
android:id="@+id/callImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
app:srcCompat="@android:drawable/sym_action_call" />
</LinearLayout>
您只需要添加 callImage
android:layout_alignParentRight="true"
并添加 suspect_button
android:layout_width="match_parent"
和 android:layout_toLeftOf="@+id/callImage"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:padding="16dp">
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/crime_title_label" />
<EditText
android:id="@+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:hint="@string/crime_title_hint" />
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/crime_title"
android:text="@string/crime_details_label" />
<Button
android:id="@+id/crime_date"
android:layout_below="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_below="@+id/crime_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_solved_label" />
<Button
android:id="@+id/suspect_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:layout_toLeftOf="@+id/callImage"
android:text="@string/choose_suspect" />
<Button
android:id="@+id/send_crime_report"
android:text="@string/send_crime_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/suspect_button" />
<ImageView
android:id="@+id/callImage"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:layout_alignBottom="@+id/suspect_button"
android:layout_alignParentRight="true"
app:srcCompat="@android:drawable/sym_action_call" />
</RelativeLayout>
所以结果会像这样
在评论中回答问题。我需要添加 LinearLayout 以使 ImageView 对齐屏幕的右半部分
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:padding="16dp">
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/crime_title_label" />
<EditText
android:id="@+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:hint="@string/crime_title_hint" />
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/crime_title"
android:text="@string/crime_details_label" />
<Button
android:id="@+id/crime_date"
android:layout_below="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_below="@+id/crime_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_solved_label" />
<LinearLayout
android:id="@+id/suspect_button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:id="@+id/suspect_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/choose_suspect" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/callImage"
android:layout_width="57dp"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/sym_action_call" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/send_crime_report"
android:text="@string/send_crime_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/suspect_button_layout" />
</RelativeLayout>
会这样显示
与 Tung Duong 的回答略有不同:
1.Align callImage
android:layout_alignParentRight="true"
2.Make suspect_button
layout_width
至 match_parent
并将 layout_marginRight
添加到 suspect_button
与 callImage
的宽度完全相同(即 57dp)
这就是现在的样子。
使用 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:padding="16dp">
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/crime_title_label" />
<EditText
android:id="@+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:hint="@string/crime_title_hint" />
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/crime_title"
android:text="@string/crime_details_label" />
<Button
android:id="@+id/crime_date"
android:layout_below="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_below="@+id/crime_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_solved_label" />
<Button
android:id="@+id/suspect_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:text="@string/choose_suspect" />
<Button
android:id="@+id/send_crime_report"
android:text="@string/send_crime_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/suspect_button" />
<ImageView
android:id="@+id/callImage"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:layout_alignBottom="@+id/suspect_button"
android:layout_toRightOf="@id/suspect_button"
app:srcCompat="@android:drawable/sym_action_call" />
</RelativeLayout>
我想要做的是,让 ImageView callImage
位于 suspect_button
的右侧,并且整体位于最右侧。为此,我希望尽可能延长 suspect_button
,就像您在 match_parent
中所做的那样。但是,当我执行 match_parent
时,ImageView 会丢失。简而言之,我想“match_parent
”suspect_button
,但我的 ImageView callImage
位于 suspect_button
的右侧,同时不会在外观上迷路。
如果您不建议需要我使用其他布局的解决方案,我将不胜感激。我想了解是否可能,或者如何在相对布局中做我想做的事情。
谢谢。
您可以在 suspect_button 中使用 layout_toLeftOf 并传入 callImage 的 ID。这样你的按钮将占据屏幕的宽度,但将为图像留出空间。
嘿伙计,你必须尝试 LinearLayout 并在两个元素中设置它的 android:layout_weight="5"!这将平均分配这两个元素!
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/send_crime_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="sdfsdfsd" />
<ImageView
android:id="@+id/callImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
app:srcCompat="@android:drawable/sym_action_call" />
</LinearLayout>
您只需要添加 callImage
android:layout_alignParentRight="true"
并添加 suspect_button
android:layout_width="match_parent"
和 android:layout_toLeftOf="@+id/callImage"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:padding="16dp">
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/crime_title_label" />
<EditText
android:id="@+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:hint="@string/crime_title_hint" />
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/crime_title"
android:text="@string/crime_details_label" />
<Button
android:id="@+id/crime_date"
android:layout_below="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_below="@+id/crime_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_solved_label" />
<Button
android:id="@+id/suspect_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:layout_toLeftOf="@+id/callImage"
android:text="@string/choose_suspect" />
<Button
android:id="@+id/send_crime_report"
android:text="@string/send_crime_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/suspect_button" />
<ImageView
android:id="@+id/callImage"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:layout_alignBottom="@+id/suspect_button"
android:layout_alignParentRight="true"
app:srcCompat="@android:drawable/sym_action_call" />
</RelativeLayout>
所以结果会像这样
在评论中回答问题。我需要添加 LinearLayout 以使 ImageView 对齐屏幕的右半部分
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:padding="16dp">
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/crime_title_label" />
<EditText
android:id="@+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:hint="@string/crime_title_hint" />
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/crime_title"
android:text="@string/crime_details_label" />
<Button
android:id="@+id/crime_date"
android:layout_below="@+id/crime_details"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_below="@+id/crime_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_solved_label" />
<LinearLayout
android:id="@+id/suspect_button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/crime_solved"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:id="@+id/suspect_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/choose_suspect" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/callImage"
android:layout_width="57dp"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/sym_action_call" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/send_crime_report"
android:text="@string/send_crime_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/suspect_button_layout" />
</RelativeLayout>
会这样显示
与 Tung Duong 的回答略有不同:
1.Align callImage
android:layout_alignParentRight="true"
2.Make suspect_button
layout_width
至 match_parent
并将 layout_marginRight
添加到 suspect_button
与 callImage
的宽度完全相同(即 57dp)