动态地向LinearLayout添加视图

时间:2017-02-17 18:42:53

标签: java android android-layout dynamic view

我一直试图解决这个问题几天,但没有任何结果。 问题在于查看sub_task_comment_item,我试图添加到另一个LinearLayout comment_container_layout,它没有显示行的所有元素。显示一些照片。

在这两种情况下,图像都会丢失。

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);

addComment(View view)

调用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_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>

视图容器,我试图添加

<?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 - 行,我试图添加。

<?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>

1 个答案:

答案 0 :(得分:1)

您的代码对我来说似乎是正确的,问题可能在于您的矢量图像或其配置。尝试使用普通图像替换矢量并使用ImageView的src属性。

交叉检查 -

  1. 在build.gradle中添加了矢量支持

    android {  
      defaultConfig {  
      vectorDrawables.useSupportLibrary = true  
      }  
    }