代码之家  ›  专栏  ›  技术社区  ›  EboMike

如何在视图中定义最大尺寸或相对尺寸?

  •  2
  • EboMike  · 技术社区  · 14 年前

    我的布局的一部分应该有两个组件:顶部的滚动视图和底部的地图视图。两个视图的大小应该正好是父视图的一半。

    我有父级的大小(我在已经有大小的时候切换到这个布局),但是我不知道如何定义最大大小。此外,我认为有一种解决方案不需要定义大小;例如,通过使用类似于Swing样式的网格布局,或者在某个地方定义权重,但我没有找到任何听起来可行的方法。

    我目前的方法是这样的,它定义了最小的大小,当滚动视图超过父视图一半的高度时,最小的大小就会明显地分开:

    布局(父级是线性布局):

    <LinearLayout
        android:id="@+id/result_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        >
    
    <ScrollView
        android:id="@+id/result_scroll"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:orientation="vertical"
        >
    
    <LinearLayout
        android:id="@+id/result"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:paddingRight="?android:attr/scrollbarSize"
        android:orientation="vertical"
    />
    
    </ScrollView>
    
    <view class="com.google.android.maps.MapView"
        android:id="@+id/mapview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
      />
    
    </LinearLayout>
    

    在我切换到代码之后(totalwidth是父级的宽度,viewheight是父级高度的一半):

    LinearLayout.LayoutParams lp =
                new LinearLayout.LayoutParams(totalWidth, viewHeight);
    scrollView.setLayoutParams(lp);
    
    1 回复  |  直到 14 年前
        1
  •  4
  •   CommonsWare    14 年前

    我的布局的一部分应该有两个组件:顶部的滚动视图和底部的地图视图。两个视图的大小应该正好是父视图的一半。

    把它们放进一个 LinearLayout . 将每个的高度设置为 0px . 将每个的权重设置为 1 .

    例如:

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

    此外,我认为有一种解决方案不需要定义尺寸……或者在某个地方定义重量

    如果你不明确地告诉它有多大(“定义大小”)或隐含地(“定义重量”),你怎么知道它应该是“恰好是父级大小的一半”?