安卓系统有类似于iPhone的struts和springs吗?

5

我一直在努力寻找Android中类似于iPhone的支柱和弹簧的等效物。不过请不要说重力 :-)

好吧,确实有重力的因素,但是请解释一下如何在LinearLayout或RelativeLayout中防止视图被推出屏幕。或者展示一些其他布局方式,使屏幕填满而不会把东西挤到看不见的地方。

在iPhone界面构建器中,我只需适当地设置支柱,使每个视图占用尽可能多的空间,但不超过可用空间。这样可以让iPhone布局很好地处理方向变化。

在Android中,我读到的主要方法似乎是创建多个布局文件夹,如layout-port和layout-land,然后在它们之间复制XML布局文件,但这似乎容易出错,而且唯一的方法是将大型自定义视图的layout_height精确设置为特定的屏幕大小和方向,以防止其推动其他按钮离开屏幕。考虑到市场上Android显示器的范围,这似乎会越来越麻烦。

以下是一个例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <ImageView android:id="@+id/imageView1" android:src="@drawable/icon"
        android:layout_height="320dip" android:layout_width="fill_parent"></ImageView>
    <SeekBar android:layout_height="wrap_content" android:id="@+id/seekBar1"
        android:layout_width="fill_parent"></SeekBar>
</LinearLayout>

这只是默认的小部件。我向你挑战,找到其他设置ImageView高度的方法,使其尽可能地填满屏幕,而不会将SeekBar挤出视线范围。尝试将Imageview android:layout_height设置为fill_parent,看看我的意思。
我已经尝试过相对布局、表格布局和各种技巧。也许我错过了一些简单的东西,但我找不到它……
我唯一能想到的解决办法(但还没有实际尝试)是在代码中检测屏幕大小和其他小部件,但我不愿意走这条路,因为好像应该有一个简单的XML布局解决方案。

3
为什么不使用android:layout_weight - user634618
根据上面的评论,你对“弹簧”的描述听起来恰好就是layout_weight的作用。 - Reuben Scratton
啊哈,非常感谢 - layout_weight 确实可以做到我想要的效果,尽管我似乎需要调整它很多才能获得我想要的效果 - 我会把这些回答标记为正确的答案,但它们是注释,嗯... - Sam Joseph
2个回答

1

尝试在设置android:layout_widthandroid:layout_height0dp(取决于父视图的android:orientation值)时使用android:layout_weight

在Android中没有更好的等效方法来实现您想要做的事情...


0

我曾经遇到过类似的问题(说实话还有更多),所以我开发了SpringLayout:https://github.com/sulewicz/springlayout。 如果我理解正确,那么我认为我已经实现了你想要的效果(图像尽可能占据空间):

<?xml version="1.0" encoding="utf-8"?>
<org.coderoller.springlayout.SpringLayout 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:background="@android:color/white" >

    <TextView
        android:id="@+id/A"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        app:layout_alignParentTop="true"
        android:text="Test" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_below="@id/A"
        app:layout_above="@+id/seekBar1"
        android:src="@drawable/ic_launcher" />

    <SeekBar
        android:id="@id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        app:layout_alignParentBottom="true" />

</org.coderoller.springlayout.SpringLayout>

这里是截图


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接