Ответ 1
Вы не можете сделать это с помощью RelativeLayout
. Попробуйте LinearLayout
и атрибут layout_weight
.
У меня есть относительный макет, и у меня есть две кнопки в нем с текстами "hello" и "world" соответственно. Я хочу, чтобы эти две кнопки лежали рядом друг с другом и одинаково занимали все доступное горизонтальное пространство.
Я попробовал следующее, но не получил ожидаемый результат
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/world" android:id="@+id/world"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/hello"
android:layout_alignTop="@+id/hello"
/>
<Button
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
Я попытался изменить андроид: layout_width обоих детей на fill_parent, но это тоже не сработало.
У меня есть рабочее решение для использования LinearLayout с layout_weight, равным 0,5 для обоих дочерних элементов, но я хотел понять, есть ли способ сделать это в самом относительном компоновке.
спасибо,
Вы не можете сделать это с помощью RelativeLayout
. Попробуйте LinearLayout
и атрибут layout_weight
.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/hello"
android:layout_toRightOf="@+id/view"
android:text="First" />
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/view"
android:text="Second" />
</RelativeLayout>
android:layout_width
был fill_parent
, результат не изменится.android:layout_alignParentRight
и android:layout_alignParentLeft
Я знаю, что это старый запрос, но, возможно, он может быть полезен для кого-то.
В вашем случае правильное решение использует Linear Layout. Но для ответа на ваш запрос и предположим, что у вас есть другой виджет в вашем макете:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvdescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="description here"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tvdescription">
<Button
android:id="@+id/world"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:text="@string/world"
android:weight="1"
/>
<Button
android:id="@+id/hello"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/hello"
android:weight="1"
/>
</LinearLayout>
Вы можете попробовать LinearLayout (горизонтально), а затем установить android: layout_weight = "1" для обеих кнопок.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
в относительной компоновке вы можете достичь этого динамически.
Button1.setWidth(width / 2);
Button2.setWidth(width / 2);
Возьмите кнопку, выровняйте ее в центре с помощью 1dp
width
и height
. Теперь поместите свою Button1 с свойством android:layout_width = fill parent
to left of center
, снова по отношению к центральной кнопке поставьте Button2 в right of center
с свойством android:layout_width = fill parent
.