android - 如何定义 View 中的最大或相对尺寸?

标签 android android-layout

我的布局的一部分应该有两个组件:顶部的 ScrollView 和底部的 MapView。两个 View 的大小都应恰好是父 View 的一半。

我有父级的大小(当我已经有大小时,我会切换到此布局),但我不知道如何定义最大大小。此外,我认为有一个解决方案不需要定义大小;例如,通过使用 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);

最佳答案

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.

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

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

如果你不明确地告诉它它有多大(“定义大小”)或隐式地告诉它有多大(“定义权重”),你怎么知道它应该是“正好是物体大小的一半”家长”?

关于android - 如何定义 View 中的最大或相对尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3955706/

相关文章:

Android抽屉导航触发区域太小

Android 启动画面 : Scale Bitmap

java - Android:动画自定义 View

android - 使用数据绑定(bind)和 MVVM 处理 onClick 事件

android - 库项目中 Android 4 以下的 GCM 问题

java - Sensor.TYPE_ORIENTATION 已弃用

android - 在包 'textsize' 中找不到属性 'android' 的资源标识符

android - 什么时候可以证明覆盖方向处理是合理的

java - Android 动态微调器更新

android - 您创建的应用数量