代码之家  ›  专栏  ›  技术社区  ›  Steven Sproat

动态添加的EditText与布局XML中定义的不匹配

  •  1
  • Steven Sproat  · 技术社区  · 10 年前

    我在TableLayout中有两个并排的LinearLayout-一个有5个按顺序添加的EditText,我有一个按钮可以将EditText添加到另一个LinearLayout。

    然而,当动态添加新的文本时,它们与第一个布局中预定义的EditTexts不匹配。

    这是我的content.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.dcss.positiveday.MainActivity"
        tools:showIn="@layout/activity_main">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:text="Positives" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/textView"
            android:text="Negatives" />
    
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:stretchColumns="*">
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <LinearLayout
                    android:id="@+id/postitiveItems"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <EditText
                        android:id="@+id/editText"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint=":)" />
    
                    <EditText
                        android:id="@+id/editText2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint=":)" />
    
                    <EditText
                        android:id="@+id/editText3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint=":)" />
    
                    <EditText
                        android:id="@+id/editText4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint=":)" />
    
                    <EditText
                        android:id="@+id/editText5"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint=":)" />
                </LinearLayout>
    
                <LinearLayout
                    android:id="@+id/negativeItems"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <EditText
                        android:id="@+id/editText6"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint=":(" />
    
                </LinearLayout>
    
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:enabled="false"
                    android:onClick="proceedClick"
                    android:text="Proceed" />
    
                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_gravity="right"
                    android:onClick="addNew"
                    android:text="New Negative" />
            </TableRow>
        </TableLayout>
    
    
    </RelativeLayout>
    

    下面是我动态添加它们的android代码:

    package com.dcss.positiveday;
    
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.AbsListView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    
    public class MainActivity extends AppCompatActivity {
        private LinearLayout _layout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            _layout = (LinearLayout) findViewById(R.id.negativeItems);
        }
    
        public void addNew(View view) {
            EditText text = new EditText(MainActivity.this);
    
            text.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
            text.setHint(":(");
    
            _layout.addView(text);
        }
    }
    

    添加的任何项目都未对齐,并且文本的偏移量不匹配。您可以看到两个LinearLayouts中的布局内的EditTexts匹配。 查看其他EditTexts中的XML声明,我似乎没有附加任何特殊的布局定位。谢谢

    enter image description here

    1 回复  |  直到 10 年前
        1
  •  2
  •   Floern themue    10 年前

    这个 EditText 您正在以编程方式添加的视图没有明确的样式集,因此它们会返回到设备的默认样式,我认为这是来自Android 4.x的样式,而 编辑文本 来自XML布局的样式是Android 5视图,导致视图看起来不同。

    我想你正在使用AppCompat。因此,您必须将AppCompat样式应用于动态添加的 编辑文本 。可以通过在构造函数中显式指定样式属性来完成此操作:

    EditText text = new EditText(MainActivity.this, null, R.attr.editTextStyle);