开发者

How to define a maximum or relative dimension in a view?

One part of my layout is supposed to have two components: A ScrollView on top, and a MapView on the bottom. Both views should be exactly half the size of the parent.

I have the size of the parent (I'm switching to this layout at a point when I already have the size), but I don't know how to define the maximum size. Besides, I figure there is a solution that doesn't require defining the size; for example by having something like a Swing-style GridLayout, or defining a weight somewhere, but I haven't found anything that sounds like it would work.

My current approach is this here, which defines the minimum size, which obviously falls apart as soon as the ScrollView grows beyond half the height of the parent:

The layout (the parent is a 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>

And the code, after I switch to it (totalWidth being the width of the parent, viewHeight half the height of the parent):

LinearLayout.LayoutParams lp =
            new LinearLayout.Layo开发者_StackOverflow社区utParams(totalWidth, viewHeight);
scrollView.setLayoutParams(lp);


One part of my layout is supposed to have two components: A ScrollView on top, and a MapView on the bottom. Both views should be exactly half the size of the parent.

Put them in a LinearLayout. Set the the height of each to 0px. Set the weight of each to 1.

For example:

<?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> 

Besides, I figure there is a solution that doesn't require defining the size...or defining a weight somewhere

If you don't tell it how big it is explicitly ("defining the size") or implicitly ("defining a weight"), how are you expecting to know it is supposed to be "exactly half the size of the parent"?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜