RelativeLayout中右侧有一个ImageView时,如何使按钮的宽度与父项匹配?

时间:2018-09-30 07:17:31

标签: android android-layout android-relativelayout

这就是现在的样子。
enter image description here

使用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_parentsuspect_button,但也要在callImage的右侧放置我的ImageView suspect_button,而又不要迷失外观。

如果您不提出需要我使用其他布局的解决方案,我将不胜感激。我想了解是否可能,或如何做相对布局中的想象。

谢谢。

4 个答案:

答案 0 :(得分:3)

您只需要添加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>

所以结果会像这样 enter image description here

回答有疑问的问题。我需要添加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>

它将显示如下 enter image description here

答案 1 :(得分:1)

嘿,您必须尝试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>

答案 2 :(得分:1)

实现这一目标的方法与同东的答案略有不同:

1。对齐callImage android:layout_alignParentRight="true"
2.使suspect_button layout_widthmatch_parent 并将layout_marginRight添加到suspect_button的宽度正好为callImage的宽度(即57dp)

答案 3 :(得分:0)

您可以在 suspect_button 中使用layout_toLeftOf并传入 callImage 的ID。这样,您的按钮将占据屏幕的宽度,但将为图像留出空间。