将动态视图添加到LinearLayout [英] Adding view to LinearLayout dynamically

查看:174
本文介绍了将动态视图添加到LinearLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

当视图像这样膨胀时,结果是:

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

如果这样膨胀,比:

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



调用 addComment(View view) AddingTaskFragment



Method that calls addComment(View view) from AddingTaskFragment

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



AddingTaskFragment



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



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 - 行,我正在尝试添加。



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>


推荐答案

你的代码对我来说至少是正确的,问题可能是与您的矢量图像或其配置。对于试图用正常图像替换矢量并使用ImageView的src属性。

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.

交叉检查 -


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

  1. Added vector support in build.gradle

android {  
  defaultConfig {  
  vectorDrawables.useSupportLibrary = true  
  }  
}  


这篇关于将动态视图添加到LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆