2016-12-26 3 views
0

References ImageСоздать новую строку внутри Linear Layout

То, что я хочу: динамически добавлять изображения внутри коробки, и если линия будет завершена затем добавить его в новую строку.

Я в настоящее время: Я могу добавить изображение внутри линейного макета, если изображение не больше ширины блока, тогда оставшееся изображение не будет отображаться.

В чем проблема: Поскольку я добавляю к линейной компоновке, и я не говорю о макете, где я должен быть добавлен. Так он добавляется в конце строки.

Решение Пробовал::

Я добавил GridView с 3 колонки внутри линейного layout.But в таком подходе, я могу понять о том, как вычислить положение, в котором я добавить изображение. Поскольку в режиме сетки мы не можем найти позицию последнего заполненного элемента. Еще одна проблема с этим подходом Мне нужно отслеживать позицию последнего элемента.

код 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> 
+0

Пожалуйста, разместите свой XML-код. –

+0

добавлен код @SnehaSarkar –

ответ

1

Вы можете использовать android:numColumns="auto_fit" атрибут вашего GridView. Он будет автоматически вписываться в соответствии с количеством изображений.

<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"/> 

Тогда вы можете добавить свой linearlayout программно в метод getview() класса адаптера. Чтобы добавить LinearLayout к вашему GridView вы можете рассмотреть следующий пример

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 (с заветным фоном) и внутри него добавить ImageView с идентификатором «ivImg». Надеюсь это поможет.

+0

Как добавить linearLayout в getview()? –

+0

Вы можете просмотреть мой код, https://docs.google.com/a/betterconnectplus.com/document/d/1iz3il5T9a8qAn3dJMeX5RqDvkBXVoHtCI_BQcYJBrxI/edit?usp=sharing –

+0

Я отредактировал свой ответ. Дай мне знать, если этого достаточно. – tahsinRupam