无法动态地添加到的EditText的LinearLayout [英] Unable to dynamically add EditText to LinearLayout

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

问题描述

我看过类似这样的很多帖子,但他们没有解决我的问题。另一个有趣的事情是,在模拟器中的EditText的地方举行(意为其他部件向下移动),但没有显示,在另一方面,真正的手机上相同的实现甚至不显示小部件的地方。我有如下的的LinearLayout 里面,我需要动态地添加的EditText 部件时,的TextView 添加团队成员点击

 <的LinearLayout
            机器人:ID =@ + ID / layout_add_task
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_below =@ ID /分离器
            机器人:layout_margin =5DP
            机器人:背景=@彩色/ yellow_background
            机器人:方向=垂直
            机器人:填充=5DP>            <的LinearLayout
                机器人:ID =@ + ID / layout_task_name
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:方向=横向>                < ImageView的
                    机器人:ID =@ + ID / iv_check_research_tasks
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:SRC =@绘制/ icon_check_empty/>                <的EditText
                    机器人:ID =@ + ID / et_task_name
                    机器人:layout_width =match_parent
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_marginLeft =10dp
                    机器人:selectAllOnFocus =真
                    机器人:文字=任务名称
                    机器人:TEXTSIZE =20dp
                    机器人:文字样式=大胆/>
            < / LinearLayout中>            <的TextView
                机器人:ID =@ + ID / tv_add_team_member
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =右
                机器人:layout_marginTop =5DP
                机器人:文字=添加团队成员
                机器人:文字颜色=@彩色/ text_green
                机器人:TEXTSIZE =15dp
                机器人:文字样式=大胆/>            <的LinearLayout
                机器人:ID =@ + ID / ll_add_task
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_below =@ ID / tv_add_team_member
                机器人:方向=横向
                机器人:weightSum =100>                < ImageView的
                    机器人:ID =@ + ID / iv_create_assignment_attach
                    机器人:layout_width =0dp
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_gravity =center_vertical
                    机器人:layout_weight =10
                    机器人:SRC =@绘制/ icon_flag_alt/>                <的TextView
                    机器人:layout_width =0dp
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_gravity =center_vertical
                    机器人:layout_marginLeft =5DP
                    机器人:layout_weight =15
                    机器人:文字=由于
                    机器人:TEXTSIZE =15dp/>                <的EditText
                    机器人:layout_width =0dp
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_weight =65
                    机器人:提示=无
                    机器人:selectAllOnFocus =真/>                < ImageView的
                    机器人:layout_width =0dp
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_gravity =center_vertical
                    机器人:layout_weight =10
                    机器人:SRC =@绘制/ icon_calendar_empty/>
            < / LinearLayout中>            <的LinearLayout
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent
                机器人:重力=CENTER_HORIZONTAL
                机器人:方向=横向>                <按钮
                    机器人:ID =@ + ID / btn_create_assignment_trash
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_marginRight =30dp
                    机器人:layout_marginTop =5DP
                    机器人:文字=回收站/>                <按钮
                    机器人:ID =@ + ID / btn_create_assignment_done
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_marginLeft =30dp
                    机器人:layout_marginTop =5DP
                    机器人:文字=完成/>
            < / LinearLayout中>
        < / LinearLayout中>

下面是Java code时, onClickListener 实施的TextView的。

  tvAddTeamMember.setOnClickListener(新OnClickListener(){        @覆盖        公共无效的onClick(视图v){
            LL的LinearLayout =(的LinearLayout)getActivity()findViewById(R.id.layout_task_name)。
            等的EditText =新的EditText(getActivity());
            LinearLayout.LayoutParams P =新LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            et.setLayoutParams(P);
            et.setText(文本);
            ll.addView(等);        }


解决方案

喜@Anas Azeem:你的code是correct.through您code,我们可以动态地添加editetxt线性布局。问题是only.in布局提到添加布局横向的方向布局文件,而不是一个使用垂直像下面

 <的LinearLayout
            机器人:ID =@ + ID / layout_task_name
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=垂直>            < ImageView的
                机器人:ID =@ + ID / iv_check_research_tasks
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:SRC =@绘制/ ic_launcher/>            <的EditText
                机器人:ID =@ + ID / et_task_name
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginLeft =10dp
                机器人:selectAllOnFocus =真
                机器人:文字=任务名称
                机器人:TEXTSIZE =20dp
                机器人:文字样式=大胆/>
        < / LinearLayout中>

I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.

<LinearLayout
            android:id="@+id/layout_add_task"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/separator"
            android:layout_margin="5dp"
            android:background="@color/yellow_background"
            android:orientation="vertical"
            android:padding="5dp">

            <LinearLayout
                android:id="@+id/layout_task_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/iv_check_research_tasks"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/icon_check_empty" />

                <EditText
                    android:id="@+id/et_task_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:selectAllOnFocus="true"
                    android:text="Task Name"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </LinearLayout>

            <TextView
                android:id="@+id/tv_add_team_member"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginTop="5dp"
                android:text="Add a team member"
                android:textColor="@color/text_green"
                android:textSize="15dp"
                android:textStyle="bold" />

            <LinearLayout
                android:id="@+id/ll_add_task"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_add_team_member"
                android:orientation="horizontal"
                android:weightSum="100" >

                <ImageView
                    android:id="@+id/iv_create_assignment_attach"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="10"
                    android:src="@drawable/icon_flag_alt" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="15"
                    android:text="Due"
                    android:textSize="15dp" />

                <EditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="65"
                    android:hint="None"
                    android:selectAllOnFocus="true" />

                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="10"
                    android:src="@drawable/icon_calendar_empty" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/btn_create_assignment_trash"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="5dp"
                    android:text="Trash" />

                <Button
                    android:id="@+id/btn_create_assignment_done"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="5dp"
                    android:text="Done" />
            </LinearLayout>
        </LinearLayout>

Here is the Java code, the onClickListener implementation of the TextView.

tvAddTeamMember.setOnClickListener(new OnClickListener() {

        @Override

        public void onClick(View v) {
            LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
            EditText et = new EditText(getActivity());
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            et.setLayoutParams(p);
            et.setText("Text");
            ll.addView(et);

        }

解决方案

Hi @Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below

<LinearLayout
            android:id="@+id/layout_task_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/iv_check_research_tasks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <EditText
                android:id="@+id/et_task_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:selectAllOnFocus="true"
                android:text="Task Name"
                android:textSize="20dp"
                android:textStyle="bold" />
        </LinearLayout>

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

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