0

LinearLayout我们可以使用 Layout_Weight 水平填充元素,这使它们占据所有宽度,每个宽度取决于它们的 Layout_Weight 值(或比率)。我的问题是在RelativeLayout. 没有像 Layout_Weight 这样的属性。我RelativeLayout在我的项目中使用 a ,它包含 4 个按钮,它们需要位于屏幕底部并且必须填满整个宽度。如果我使用硬编码Layout_Width="20dp"或类似的东西。当我将方向从纵向更改为横向时,它会产生问题,反之亦然。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <Button
            android:id="@+id/feed_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="@string/feed"
            android:textColor="#FF000000" />

        <Button
            android:id="@+id/iwant_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@id/feed_button"
            android:text="@string/iwant"
            android:textColor="#FF000000" />

        <Button
            android:id="@+id/share_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@id/iwant_button"
            android:text="@string/share"
            android:textColor="#FF000000" />

        <Button
            android:id="@+id/profile_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@id/share_button"
            android:text="@string/profile"
            android:textColor="#FF000000" />


</RelativeLayout>
4

4 回答 4

0

用于使相对布局占据全宽

android:layout_width="fill_parent"

将其添加到底部

android:layout_alignParentBottom="true"
于 2012-04-04T10:07:44.173 回答
0

我认为没有像layout_weightin那样的属性Relative layout。但是 android 已经提供了Linear Layout. 在那种情况下,你为什么要和Relative layout?

我想你知道如何使用layout_weightin Linear Layout

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" >

    <Button
        android:id="@+id/feed_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="feed"
        android:textColor="#FF000000" />

    <Button
        android:id="@+id/iwant_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="iwant"
        android:textColor="#FF000000" />


</LinearLayout>

我知道你有强制使用相对布局但是如果你把上面的代码放在你的活动中,它会在显示的底部显示所有按钮,如果想在任何其他布局的下方显示这些按钮布局,那么android:layout_below attributte<LinearLayout >标签中使用并将最外面的布局用作相对布局

谢谢

于 2012-04-04T10:09:38.520 回答
0

确保按钮全部相等的唯一方法是将它们放在一个LinearLayout填充您希望按钮覆盖的区域的区域中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

   <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:orientation="vertical" >
        <Button
            android:id="@+id/feed_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/feed"
            android:textColor="#FF000000" />

        <Button
            android:id="@+id/iwant_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:layout_toRightOf="@id/feed_button"
            android:text="@string/iwant"
            android:textColor="#FF000000" />

        <Button
            android:id="@+id/share_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/share"
            android:textColor="#FF000000" />

        <Button
            android:id="@+id/profile_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="@string/profile"
            android:textColor="#FF000000" />
  </LinearLayout>

</RelativeLayout>

LinearLayout填充父布局的整个宽度,并根据它们的重量平均划分按钮。

如果您有两个想要平均划分的布局,则有一个非常巧妙的技巧RelativeLayout。创建一个 0x0 像素的“不可见”布局,将其对齐到父视图的中心,然后将两个布局对齐到相对于您想要它们的位置的不可见布局。

于 2012-04-04T13:33:55.940 回答
0

在相对布局中,我们可以动态地实现这一点。

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int height = metrics.heightPixels;
int width = metrics.widthPixels;

Button1.setWidth(width / 2);
Button2.setWidth(width / 2);
于 2012-05-07T05:24:16.727 回答