2

我正在尝试实现这样的目标:

 -  |-------------------|<-------Phone screen
 |  ||-----------------||
 |  ||                 ||
80% ||                 ||
 |  ||                 ||
 |  ||                 |<--------Image View  (fills screen, background)
 -  |||---------------|||
 |  |||               |||
20% |||               |<---------Scroll View (starts after 80% of parent view)
 |  ||-----------------||
 -  |-------------------|  

ImageView 有一个填充背景的图像,并且 scrollView 在高度的 80% 之后开始。有人可以帮助我了解如何为所有设备尺寸实现这一目标吗?

4

2 回答 2

3

利用layout_weight

<RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

<LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="8" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2">

        </ScrollView>

</LinearLayout>

</RelativeLayout>
于 2013-09-22T10:50:50.130 回答
1

你需要一个线性布局,android:orientation="vertical".

然后将两个视图 (和ImageView)ScrollView添加android:layout_weight="80"ImageViewandroid:layout_weight="20"ScrollView

两者都必须android:layout_height="0dip"确定,只有重量很重要。

XML 代码如下所示(我使用了第二个 ImageView 而不是 ScrollView 来以蓝色和红色显示结果)

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

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="80"
        android:src="#FF0000FF" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="20"
        android:src="#FFFF0000" />

编辑:要用图像填充背景,您有两种方法:

  • 的简单集合android:backround="<your drawable resoure>"LinearLayout您唯一可能会松动的是可以在背景中居中和调整可绘制对象的大小。ImageView 在这一点上有更多的可能性。
  • 因此,第二种方法是使用FrameLayout. 它很轻巧,基本上根本没有定位。只需将所有元素放在一起。XML 如下所示:

        <!-- This is your fixed view, e.g. with header information -->
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="80"
            android:src="@android:color/transparent" />
    
        <!--  This would be your scroll view, e.g. for detail information -->
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="20"
            android:src="#FFFF0000" />
    

于 2013-09-22T10:49:01.767 回答