【问题标题】:How to do this layout? (Link on Image)这个布局怎么做? (图片上的链接)
【发布时间】:2010-04-12 06:32:38
【问题描述】:

我想知道如何获得这个结果:

http://img718.imageshack.us/img718/6173/screenshot20100411at126.png

这是我尝试过的:

<RelativeLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="#CCCCCC"
                  android:orientation="horizontal"
                  android:layout_weight="0">

        <Button android:id="@+id/btn_cancel"
                android:text="@string/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5px"
                android:gravity="left"
                />

        <Button android:id="@+id/btn_save"
                android:text="@string/save"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5px"
                android:layout_toRightOf="@id/btn_cancel"
                android:gravity="right"/>
    </RelativeLayout>

我尝试使用 layout_width 属性并将它们设置为填充父母双方但没有奏效,如果有人有想法...谢谢!

【问题讨论】:

标签: android android-layout


【解决方案1】:
<LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:padding="5dip"
              android:background="#CCCCCC"
              android:orientation="horizontal">

    <Button android:id="@+id/btn_cancel"
            android:text="@string/cancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip"
            android:layout_weight="1"/>

    <Button android:id="@+id/btn_save"
            android:text="@string/save"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
</LinearLayout>

LinearLayout 中让两个小部件占用相同空间的技巧是在它们两个上设置layout_width="fill_parent",然后在它们两个上设置layout_weight="1"。将两者都设置为 fill_parent 和相同的 layout_weight 将告诉 LinearLayout 在两者之间分割所有可用空间。

另外,请使用dip,而不是pxdip 与屏幕尺寸无关,在不同尺寸的屏幕上看起来会更好。

【讨论】:

  • 感谢下面的代码和解释。很高兴知道。
猜你喜欢
  • 1970-01-01
  • 2013-09-11
  • 2017-08-03
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 2020-10-04
相关资源
最近更新 更多