Android 如何制作按钮';在RelativeLayout的右侧有一个ImageView时,其宽度是否与父级匹配?
这就是它现在的样子。Android 如何制作按钮';在RelativeLayout的右侧有一个ImageView时,其宽度是否与父级匹配?,android,android-layout,android-relativelayout,Android,Android Layout,Android 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" androi
使用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>
我想做的是,让ImageViewcallImage
位于suspect_按钮的右侧,并位于最右侧。为此,我希望尽可能扩展suspect_按钮
,就像在match_parent
中一样。但是当我匹配父对象时,ImageView就会丢失。因此,简单地说,我想“匹配父项”可疑按钮
,但同时在可疑按钮
的右侧有我的ImageViewcallImage
,同时不迷失外观
如果您不建议需要我使用其他布局的解决方案,我将不胜感激。我想知道这是否可能,或者如何在相对布局中做我想象中要做的事情
谢谢。您可以在“可疑”按钮中使用layout_toLeftOf,并输入callImage的id。这样,您的按钮将占据屏幕的宽度,但会为图像留出空间。您可以在可疑按钮中使用layout\u toLeftOf,并传入callImage的id。这样,您的按钮将占据屏幕的宽度,但会为图像留出空间。嘿,伙计,您必须尝试LinearLayout并在两个元素中设置其android:layout\u 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>
嘿,伙计,你必须尝试LinearLayout,并在两个元素中设置其android:layout\u 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\u alignParentRight=“true”
和添加可疑按钮
android:layout\u width=“match\u parent”
和android:layout\u toLeftOf=“@+id/callImage”
所以结果会是这样的
在评论中回答问题。我需要添加线性布局,使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>
它会像这样表现出来
您只需添加
callImage
android:layout\u alignParentRight=“true”和添加可疑按钮android:layout\u width=“match\u parent”
和android:layout\u toLeftOf=“@+id/callImage”
所以结果会是这样的
在评论中回答问题。我需要添加线性布局,使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>
它会像这样表现出来
实现这一目标的方法与董栋的答案略有不同:
1.对齐callImage
android:layout\u alignParentRight=“true”
2.使可疑按钮布局宽度与匹配\u父项
并将layout\u marginRight
添加到suspect\u按钮
,与callImage
(即57dp)的宽度完全相同实现这一点的方法与董栋的答案略有不同:
1.对齐callImage
android:layout\u alignParentRight=“true”
2.使可疑按钮布局宽度与匹配\u父项
并将layout\u marginRight
添加到suspect\u按钮
,与callImage(即57dp)的宽度完全相同这确实有效。但是我不明白为什么5意味着这两个元素将有相等的部分,这是有效的。但是我不明白为什么5意味着两个元素有相等的部分