【问题标题】:GridView Height issue?GridView高度问题?
【发布时间】:2017-11-24 10:51:10
【问题描述】:

我想实现下面的布局。

问题:无论屏幕大小如何,此布局中的所有视图组都应在单个屏幕中可见,无需滚动。

我做了什么:我使用 Gridview 进行底部选项卡布局。图像布局高度是动态的,因为我必须保持 16:9 的比例。

我得到了什么:

我的底部布局是网格视图正在滚动。我的网格视图项目 是方形的。

代码:

GridViewItem

<com.app.phlebo.customViews.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ll_profile"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="bottom"
    android:background="@drawable/curved_edge"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <com.app.phlebo.customViews.TextViewPlus
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="37dp"
        android:layout_below="@+id/img_icon"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="@dimen/text_size"
        app:fonts="Montserrat-Light.otf" />
</com.app.phlebo.customViews.MyLinearLayout>

我的自定义线性布局(用于网格视图项)

public class MyLinearLayout extends LinearLayout

    {
    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int width = getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
        setMeasuredDimension(width, width);
    }
}

我的 content_main.xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:showIn="@layout/activity_main"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/curve_edge_transparent"
            android:id="@+id/rl">

            <include layout="@layout/layout_welcome"
                android:id="@+id/layout_dots"/>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp">

            <RelativeLayout
                android:id="@+id/sp_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:background="@drawable/cornor_layout">


                <com.app.phlebo.customViews.TextViewPlus
                    android:layout_width="match_parent"
                    android:layout_height="35dp"
                    android:textSize="@dimen/text_size"
                    android:gravity="center_vertical"
                    android:textColor="@color/black"
                    android:id="@+id/spinner"
                    android:padding="5dp"
                    android:text="Today"
                    app:fonts="Montserrat-Medium.otf" />

                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="35dp"
                    android:id="@+id/img_dots"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:gravity="center_vertical"
                    android:paddingRight="5dp"
                    android:src="@drawable/more_line" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_below="@+id/spinner"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"
                    android:layout_marginTop="5dp"
                    android:id="@+id/view_middle"
                    android:background="#E5E5E5"/>

                <RelativeLayout
                    android:id="@+id/rl_middle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/view_middle">

                    <View
                        android:id="@+id/center_shim"
                        android:layout_width="2dp"
                        android:layout_height="20dp"
                        android:layout_centerInParent="true"
                        android:layout_marginBottom="10dp"
                        android:layout_marginTop="10dp"
                        android:background="#E5E5E5" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/ll_accepted_request"
                        android:layout_centerInParent="true"
                        android:layout_toLeftOf="@+id/center_shim"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:padding="2dp">

                        <com.app.phlebo.customViews.TextViewPlus
                            android:id="@+id/tv_count_accepted_request"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_toLeftOf="@+id/center_shim"
                            android:gravity="center"
                            android:text=""
                            android:textColor="@color/colorPrimary"
                            android:textSize="25sp"
                            app:fonts="Montserrat-SemiBold.otf" />


                        <com.app.phlebo.customViews.TextViewPlus
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:text="Accepted Request"
                            android:textColor="@android:color/black"
                            android:textSize="@dimen/text_size"
                            app:fonts="Montserrat-Light.otf"/>
                    </LinearLayout>


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:id="@+id/ll_pending_request"
                        android:layout_toRightOf="@+id/center_shim"
                        android:gravity="center"
                        android:orientation="vertical"
                        android:padding="2dp">

                        <com.app.phlebo.customViews.TextViewPlus
                            android:id="@+id/tv_count_rejected_request"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:text=""
                            android:textColor="@color/dashboard_red"
                            android:textSize="25sp"
                            app:fonts="Montserrat-SemiBold.otf"/>

                        <com.app.phlebo.customViews.TextViewPlus
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:text="Pending Request"
                            app:fonts="Montserrat-Light.otf"
                            android:textColor="@android:color/black"
                            android:textSize="@dimen/text_size" />

                    </LinearLayout>
                </RelativeLayout>

            </RelativeLayout>



            <GridView
                android:id="@+id/gridview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/sp_date"
                android:layout_alignParentBottom="true"
                android:layout_marginTop="3dp"
                android:numColumns="3"
                android:gravity="center"
                android:horizontalSpacing="5dp"
                android:verticalSpacing="5dp"
                />

        </RelativeLayout>
    </LinearLayout>

请帮我实现上述布局。我要全部单身 没有滚动的屏幕。

【问题讨论】:

标签: android android-linearlayout android-gridview android-scrollview


【解决方案1】:

解决方案非常简单。 您已使用“com.app.phlebo.customViews.MyLinearLayout”作为父布局。但它扩展了线性布局。 线性布局默认是可滚动的。

因此,为了避免网格组件位于屏幕下方(包含在屏幕内) 将 Image View 的高度更改为特定高度。(添加网格组件后的剩余高度) 注意:我假设网格组件的数量是固定的。其他

<ImageView
    android:id="@+id/img_icon"
    android:layout_width="wrap_content"
    android:layout_height="200dp"  // Find height needed and set
    android:layout_centerInParent="true" />

【讨论】:

  • 我的网格行项目有图像视图和文本视图。它必须在某个容器中。
  • 是的,我明白你的意思,但我的意思是,因为它在线性布局中(默认情况下可滚动),你的其他文本视图会根据顶部图像的高度改变位置。 (这是你的问题的原因)..不需要改变线性布局,只需为 imageView 固定一个合适的高度。希望它有所帮助:-)
猜你喜欢
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 2011-11-29
相关资源
最近更新 更多