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

时间:2010-10-17 23:06:33

标签: android android-layout

我的布局的一部分应该有两个组件:顶部的ScrollView和底部的MapView。两个视图的大小应该是父级的一半。

我有父母的大小(我已经在我已经有大小的时候切换到这个布局),但我不知道如何定义最大大小。此外,我认为有一个解决方案不需要定义大小;例如,通过使用Swing风格的GridLayout,或者在某个地方定义一个重量,但是我没有找到任何听起来像是可行的东西。

我目前的方法是这里,它定义了最小尺寸,一旦ScrollView增长超过父母高度的一半,它就会明显崩溃:

布局(父级是LinearLayout):

<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 个答案:

答案 0 :(得分:4)

  

我的布局的一部分应该有两个组件:顶部的ScrollView和底部的MapView。两个视图的大小应该是父级的一半。

将它们放入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> 
  

此外,我认为有一个解决方案不需要定义大小......或在某处定义权重

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