android 使视图包裹内容但不大于特定大小
android make view to wrap content but not larger than a specific size
我有一个相对视图,它有两个子视图,一个滚动视图,下面有一个按钮,现在我想这样做:我希望这个视图包装它的内容,而不是空的 space,也不超过屏幕尺寸。
我怎样才能做到这一点?有人可以帮忙吗?
这是我当前无法使用的布局文件。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</ScrollView>
<Button
android:id="@+id/action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/content"
... />
</RelativeLayout>
您应该使用 LinearLayout 并根据 weight.In 调整两个视图,这样它就不会超过任何分辨率,并且会根据屏幕大小进行调整。
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ScrollView
android:fillViewPort="true"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
</ScrollView>
<Button
android:id="@+id/action"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="@id/content"
... />
</LinearLayout>
RelativeLayout
没有这种可能。然而 LinearLayout
确实如此。只需将 LinearLayout
的 android:orientation
设置为 vertical
,将 ScrollView
的 android:layout_weight
设置为 1
,它应该可以工作:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ScrollView>
<Button
android:id="@+id/action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
您应该使操作按钮在底部对齐,并且滚动视图应位于该按钮上方。这样滚动视图将始终在屏幕上,按钮也将可见。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
... />
<ScrollView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_above="@id/action"
android:layout_height="match_parent">
....
</ScrollView>
</RelativeLayout>
我有一个相对视图,它有两个子视图,一个滚动视图,下面有一个按钮,现在我想这样做:我希望这个视图包装它的内容,而不是空的 space,也不超过屏幕尺寸。 我怎样才能做到这一点?有人可以帮忙吗?
这是我当前无法使用的布局文件。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</ScrollView>
<Button
android:id="@+id/action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/content"
... />
</RelativeLayout>
您应该使用 LinearLayout 并根据 weight.In 调整两个视图,这样它就不会超过任何分辨率,并且会根据屏幕大小进行调整。
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ScrollView
android:fillViewPort="true"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
</ScrollView>
<Button
android:id="@+id/action"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="@id/content"
... />
</LinearLayout>
RelativeLayout
没有这种可能。然而 LinearLayout
确实如此。只需将 LinearLayout
的 android:orientation
设置为 vertical
,将 ScrollView
的 android:layout_weight
设置为 1
,它应该可以工作:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ScrollView>
<Button
android:id="@+id/action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
您应该使操作按钮在底部对齐,并且滚动视图应位于该按钮上方。这样滚动视图将始终在屏幕上,按钮也将可见。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/action"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
... />
<ScrollView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_above="@id/action"
android:layout_height="match_parent">
....
</ScrollView>
</RelativeLayout>