在线性布局内创建新行

在线性布局内创建新行

本文介绍了在线性布局内创建新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

References Image

我想要的:动态地将图像添加到框内,如果一行完成,则将其添加到新行.

What I want: dynamically add the image inside the box and if a line is complete then add it to new line.

我现在在这里:我可以在线性布局中添加图像,如果没有图像大于块宽度,则不会显示其余图像.

WHere I am currently: I am able to add the image inside the linear layout, if no of image is greater than the block width then remaining image will not shown.

问题是什么:由于我要添加线性布局,所以我没有告诉布局应该在哪里添加.所以它添加在行的末尾.

What is the issue: Since I am adding to the linear layout and I am not telling the layout where I should be added. SO it add at the end of the row.

解决方案已尝试::

我在线性布局中添加了带有3列的gridview,但是通过这种方法,我能够弄清楚如何计算添加图像的位置.由于在网格视图中,我们无法找到最后填充的项目的位置.这种方法的另一个问题是我需要跟踪最后一个项目的位置.

I have added the gridview with the 3 column inside the linear layout.But in this approach, I am able to figure about how to calculate the position where I add image. Since in grid view we can't find the position of last item filled.One more problem with this approach I need to track last item position.

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/relativeView"
    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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.Toolbar>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:paddingTop="@dimen/activity_horizontal_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout

            android:weightSum="2"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:textStyle="bold"
                android:textColor="@color/green"
                android:gravity="center_horizontal"
                android:layout_weight="1"
                android:text="Morning"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <TextView
                android:textStyle="bold"
                android:textColor="@color/green"
                android:gravity="center_horizontal"
                android:layout_weight="1"
                android:text="Afternoon"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:layout_marginEnd="@dimen/activity_vertical_margin"
                android:background="@drawable/rectangle_curve"
                android:orientation="horizontal"
                android:padding="@dimen/activity_vertical_margin"
                android:id="@+id/ll1"
                android:layout_width="match_parent"
                 android:layout_height="match_parent"
                android:layout_weight="1.0">

               <!-- <GridView
                    android:layout_weight="1.0"
                    android:layout_marginTop="@dimen/cardview_default_elevation"
                    android:orientation="horizontal"
                    android:layout_marginBottom="@dimen/activity_vertical_margin"
                    android:layout_marginEnd="@dimen/activity_vertical_margin"
                    android:background="@drawable/rectangle_curve"
                    android:horizontalSpacing="10dp"
                    android:verticalSpacing="10dp"
                    android:numColumns="auto_fit"
                    android:id="@+id/gv1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    </GridView>-->
            </LinearLayout>

            <LinearLayout
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:layout_marginStart="@dimen/activity_vertical_margin"
                android:background="@drawable/rectangle_curve"
                android:padding="@dimen/activity_vertical_margin"
                android:orientation="horizontal"
                android:id="@+id/ll2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.0">

                <!--<GridView
                    android:layout_weight="1.0"
                    android:layout_marginTop="@dimen/cardview_default_elevation"
                    android:orientation="horizontal"
                    android:layout_marginBottom="@dimen/activity_vertical_margin"
                    android:layout_marginEnd="@dimen/activity_vertical_margin"

                    android:background="@drawable/rectangle_curve"
                    android:horizontalSpacing="10dp"
                    android:verticalSpacing="10dp"
                    android:numColumns="auto_fit"
                    android:id="@+id/gv2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                </GridView>
-->

            </LinearLayout>


         </LinearLayout>


        <LinearLayout
            android:weightSum="2"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:textStyle="bold"
                android:textColor="@color/green"
                android:gravity="center_horizontal"
                android:layout_weight="1"
                android:text="Evening"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <TextView
                android:textStyle="bold"
                android:textColor="@color/green"
                android:gravity="center_horizontal"
                android:layout_weight="1"
                android:text="Night"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </LinearLayout>



    <LinearLayout
        android:weightSum="2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:layout_marginEnd="@dimen/activity_vertical_margin"
            android:orientation="horizontal"
            android:padding="@dimen/activity_vertical_margin"
            android:background="@drawable/rectangle_curve"
            android:id="@+id/ll3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0">

<!--
        <GridView
            android:layout_weight="1.0"
            android:layout_marginTop="@dimen/cardview_default_elevation"
            android:orientation="horizontal"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:layout_marginEnd="@dimen/activity_vertical_margin"

            android:background="@drawable/rectangle_curve"
            android:horizontalSpacing="10dp"
            android:verticalSpacing="10dp"
            android:numColumns="auto_fit"
            android:id="@+id/gv3"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </GridView>-->

            </LinearLayout>

            <LinearLayout
                android:background="@drawable/rectangle_curve"
                android:layout_marginStart="@dimen/activity_vertical_margin"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:orientation="horizontal"
                android:id="@+id/ll4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.0">


           <!-- <GridView
            android:layout_weight="1.0"
            android:layout_marginTop="@dimen/cardview_default_elevation"
            android:orientation="horizontal"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:layout_marginEnd="@dimen/activity_vertical_margin"
            android:background="@drawable/rectangle_curve"
            android:horizontalSpacing="10dp"
            android:verticalSpacing="10dp"
            android:numColumns="auto_fit"
            android:id="@+id/gv4"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </GridView>
-->
                </LinearLayout>


        </LinearLayout>


</LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:id="@+id/fab_add_reminders"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_add"
        app:backgroundTint="@color/white"
        android:layout_gravity="bottom|end" />

</android.support.design.widget.CoordinatorLayout>

推荐答案

您可以在网格视图中使用android:numColumns="auto_fit"属性.它将根据图像数量自动适应.

You may use android:numColumns="auto_fit" attribute to your gridview. It will automatically fit according to the number of images.

 <GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gvFrameBook"
        android:numColumns="auto_fit"
        android:columnWidth="150dp"
        android:layout_centerInParent="true"
        android:stretchMode="spacingWidthUniform"
        android:verticalSpacing="5dp"/>

然后,您可以在适配器类的getview()方法中以编程方式添加您的linearlayout.要将线性布局添加到您的gridview中,您可以考虑以下示例

Then you can add your linearlayout programmatically in getview() method of adapter class. To add linearlayout to your gridview you may consider following example

 public View getView(int position, View convertView, ViewGroup parent) {
        convertView = getActivity().getLayoutInflater().inflate(R.layout.put_linearlayout_here, parent, false);
        ivImg = (ImageView) convertView.findViewById(R.id.ivImg);}

在您的"put_linearlayout_here.xml"内部添加linearlayout(具有您珍爱的background),并在其内部添加一个ID为"ivImg"的imageview.希望这可以帮助.

Inside your "put_linearlayout_here.xml" you add linearlayout (with your cherished background ) and inside it add an imageview with id "ivImg". Hope this helps.

这篇关于在线性布局内创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!