【问题标题】:How to set a layout to fit remaining space on the screen?如何设置布局以适应屏幕上的剩余空间?
【发布时间】:2017-05-01 14:49:48
【问题描述】:

我有一个由各种相对和线性布局组成的活动布局。前 2 个是顶部工具栏和具有固定大小的图像视图。下面是由一排按钮和一个水平网格视图组成的布局。 我正在尝试通过调整按钮和网格视图的大小来适应屏幕上的剩余空间。 我尝试了各种相对和线性布局的组合,使用权重,但效果不佳,导致 gridview 溢出屏幕底部。 唯一可行的解​​决方案是将固定数字设置为带有按钮的布局大小,但这在不同大小的屏幕上不起作用。

有什么想法吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1"
android:fillViewport="true"
android:orientation="vertical"
tools:context="...">

<include
    android:id="@+id/top_toolbar_details"
    layout="@layout/toolbar" />

<LinearLayout
    android:id="@+id/ll_details"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/top_toolbar_details"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_view_illusion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:keepScreenOn="true"
        android:longClickable="true"
        android:scaleType="centerInside"
        android:src="@drawable/motion1" />

</LinearLayout>

<LinearLayout
    android:id="@+id/ll_bottom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/ll_details">

    <LinearLayout
        android:id="@+id/rl_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/b_last_viewed"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:background="@drawable/circle_shape_drawable"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_previous" />

        <ImageButton
            android:id="@+id/b_all_illusions"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:adjustViewBounds="true"
            android:background="@drawable/circle_shape_drawable"
            android:cropToPadding="false"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_grid" />

        <ImageButton
            android:id="@+id/b_to_favourites"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:background="@drawable/circle_shape_drawable"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_favourite" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_horizontal_grid_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <android.support.v17.leanback.widget.HorizontalGridView
            android:id="@+id/gv_small_preview"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    你必须限制你的布局,

    <LinearLayout
        android:id="@+id/ll_horizontal_grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/ll_bottom"
        android:layout_below"@id/ll_details"
        android:orientation="vertical">
    
        <android.support.v17.leanback.widget.HorizontalGridView
            android:id="@+id/gv_small_preview"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>
    

    也就是说,您的布局相当复杂,最好用 ConstraintLayout 替换根布局。

    【讨论】:

    • 哦,这太酷了!不知道这个(对android来说还是很新),它似乎运作良好。谢谢。
    【解决方案2】:

    试试这个

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/ll_details">
    
        <LinearLayout
            android:id="@+id/rl_buttons"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">
    
            <ImageButton
                android:id="@+id/b_last_viewed"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:background="@drawable/circle_shape_drawable"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_previous" />
    
            <ImageButton
                android:id="@+id/b_all_illusions"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="0dp"
                android:layout_marginRight="0dp"
                android:adjustViewBounds="true"
                android:background="@drawable/circle_shape_drawable"
                android:cropToPadding="false"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_grid" />
    
            <ImageButton
                android:id="@+id/b_to_favourites"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:background="@drawable/circle_shape_drawable"
                android:scaleType="fitCenter"
                android:src="@drawable/ic_favourite" />
    
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/ll_horizontal_grid_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">
    
            <android.support.v17.leanback.widget.HorizontalGridView
                 android:id="@+id/gv_small_preview"
                 android:layout_width="wrap_content"
                 android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    • 感谢建议,试过了,好一点,但还是有点溢出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多