android - 这个布局该怎么做呢? (图片上的链接)

标签 android android-layout

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

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属性并将它们设置为填充 parent 双方,但没有成功,如果有人有想法......谢谢!

最佳答案

<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 与屏幕尺寸无关,并且在不同尺寸的屏幕上看起来会更好。

关于android - 这个布局该怎么做呢? (图片上的链接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2620246/

相关文章:

android - 在 Android 中自定义工具栏的问题

android - 使用软键盘向上移动一种布局,但将一种布局保留在底部

android - Mediaplayer 如何避免在 RecyclerView 中播放多个音频

java - 解析 android 屏幕转储文件

android - 图像查看器 fragment (Android)

android - 如何为两个分辨率不同但尺寸相同的平板电脑设置布局?

Android 通知 Activity 不会启动

android - 重新打开 sqlcipher DB 时创建表 android_metadata 失败

android - 分页列表和 CursorLoader

Android Studio "Build - make project"不创建jar