Как сделать так, чтобы ширина кнопки соответствовала родителю, когда справа от ReViewLayout находится ImageView? - PullRequest
0 голосов
/ 30 сентября 2018

Так вот как это выглядит прямо сейчас.
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_parent" suspect_button, но при этом мой ImageView callImage находится на правой стороне suspect_button, при этом не теряясь во внешнем виде.

Буду признателен, если вы не предложите решения, требующие использования другого макета.Я хочу узнать, возможно ли это, или как сделать то, что я представляю, чтобы сделать в Relative Layout.

Спасибо.

Ответы [ 4 ]

0 голосов
/ 30 сентября 2018

Немного другой способ добиться этого, чем ответ Тунг Дуонга:

1. Выровнять callImage android:layout_alignParentRight="true"
2. Сделать suspect_button layout_width на match_parent и добавить layout_marginRightsuspect_button в точности как ширина callImage (т.е. 57dp)

0 голосов
/ 30 сентября 2018

Эй, чувак, вы должны попробовать 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>
0 голосов
/ 30 сентября 2018

Вам просто нужно добавить 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

0 голосов
/ 30 сентября 2018

Вы можете использовать layout_toLeftOf в вашем suspect_button и передать идентификатор callImage .Таким образом, ваша кнопка займет ширину экрана, но оставит место для изображения.

...