简体   繁体   中英

Adding view to LinearLayout dynamically

I've been trying to solve this problem for couple of days, but haven't got any result. The problem is in View sub_task_comment_item , that I'm trying to add to another LinearLayout comment_container_layout , it's not showing all elements of row. Showing some pictures bellow.

In both cases image are lost.

When view is inflated like this, result is:

final View subTaskView = layoutInflater.inflate(R.layout.sub_task_comment_item, comment_layout, false);

If inflated like this, than:

 final View subTaskView = layoutInflater.inflate(R.layout.sub_task_comment_item, null);

Method that calls addComment(View view) from AddingTaskFragment

 public void onAddCommentButtonClicked(View view)
    {
        addingTaskFragment.addComment(view);
    }

AddingTaskFragment

public class AddingTaskFragment extends Fragment {

 private Activity activity;

    //Comments
    private CheckBox comments_available;
    private LinearLayout comment_container_layout;
    private LinearLayout comment_layout;

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        this.activity = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View viewHierarchy = inflater.inflate(R.layout.fragment_new_task_type, container, false);

        comments_available = (CheckBox) viewHierarchy.findViewById(R.id.use_comment_checkBox);
        comment_container_layout = (LinearLayout)viewHierarchy.findViewById(R.id.comment_container_layout);
        comment_layout = (LinearLayout)viewHierarchy.findViewById(R.id.comment_container);

        isCommentsAvailable();

        return viewHierarchy;
    }

    private void isCommentsAvailable()
    {
        comments_available.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b)
            {
                if(b)
                {
                    comment_container_layout.setVisibility(View.VISIBLE);
                }
                else comment_container_layout.setVisibility(View.GONE);
            }
        });
    }

    public void addComment(View view)
    {
          switch (view.getId())
          {
              case button_add_comment :
                  LayoutInflater layoutInflater = (LayoutInflater) activity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 final View subTaskView = layoutInflater.inflate(R.layout.sub_task_comment_item, comment_layout, false);

                  if (comment_layout != null)
                  {
                      comment_layout.addView(subTaskView);
                  }

                  ImageView buttonRemove = (ImageView) subTaskView.findViewById(R.id.remove_sub_task_icon);

                  final View.OnClickListener thisListener = new View.OnClickListener()
                  {
                      @Override
                      public void onClick(View v)
                      {
                          ((LinearLayout)subTaskView.getParent()).removeView(subTaskView);
                      }
                  };

                  buttonRemove.setOnClickListener(thisListener);

                  break;
          }
    }
}

Fragment layout - fragment_new_task_type

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true">

    <TextView
        android:id="@+id/task_type"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:text="No type selected"
        android:gravity="center_vertical"
        android:textSize="34sp"
        style="@style/TextRobotoRegular"
        android:textColor="@color/colorTitleText"/>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="12dp"
        style="@style/StyledTilEditTextLayout"
        app:hintTextAppearance="@style/StyledTilEditTextFloatingLabel">

        <EditText
            android:id="@+id/task_type_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/task_type_hint"
            android:inputType="text"
            android:maxLines="1"
            android:textSize="24sp"
            android:maxLength="38"
            style="@style/StyledTilEditText"/>

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

<include layout="@layout/layout_task_title_input" />
<include layout="@layout/layout_task_date_input" />
<include layout="@layout/layout_task_start_time_input" />
<include layout="@layout/layout_task_end_time_input" />
<include layout="@layout/layout_task_comment_input" />
<include
    android:id="@+id/comment_container_layout"
    layout="@layout/layout_task_comment_container"
    android:visibility="gone"/>

    <include layout="@layout/layout_task_sub_tasks_input" />
    <include layout="@layout/layout_task_sub_tasks_container"
        android:visibility="gone"/>
    <include layout="@layout/layout_task_importance_input" />
    <include layout="@layout/layout_task_importance"
        android:visibility="gone"/>
    <include layout="@layout/layout_task_interests_input" />
    <include layout="@layout/layout_task_interests"
        android:visibility="gone"/>

</LinearLayout>

container for view, I'm trying to add

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16dp"
    android:orientation="vertical">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:id="@+id/button_add_comment"
        android:onClick="onAddCommentButtonClicked">

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingRight="16dp"
           style="@style/TextRobotoRegular"
           android:textSize="16sp"
           android:text="@string/add_comment_task_hint"
           android:textColor="@color/colorTitleText"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_add_task" />

        </LinearLayout>

    <LinearLayout
        android:id="@+id/comment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:orientation="vertical">

        </LinearLayout>

</LinearLayout>

sub_task_comment_item - row, that I'm trying to add.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:background="@color/colorGreyText10"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/sub_item_number"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="1."
        android:layout_weight="1"
        android:textSize="16sp"
        style="@style/TextRobotoLight"/>

    <TextView
        android:id="@+id/sub_item_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:text="Make a design"
        android:textSize="16sp"
        style="@style/TextRobotoLight"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/ic_remove_task"
        android:id="@+id/remove_sub_task_icon" />

</LinearLayout>

Your code seems correct atleast to me, issue might be with your vector image or with its configuration. For a try replace vector with normal image and use src propery of ImageView.

Cross check -

  1. Added vector support in build.gradle

     android { defaultConfig { vectorDrawables.useSupportLibrary = true } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM