代码之家  ›  专栏  ›  技术社区  ›  Madeleine Smith

超时后添加视图会导致其他视图的填充出错-Android

  •  0
  • Madeleine Smith  · 技术社区  · 8 年前

    我有一个问题,当我在延迟后添加视图时,当我单击字母表按钮时,这会改变它们的填充。

    主要活动:

    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LinearLayout linearLayout = findViewById(R.id.lettersToGuess);
    
                LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                TextView asdfsdfanotherTV = (TextView) layoutInflater.inflate(R.layout.guessing_letter_text_view, linearLayout, false);
    
                linearLayout.addView(asdfsdfanotherTV);
            }
        }, 1000);
    }
    
    
    }
    

    activity\u main。xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:background="@color/white"
        tools:context=".MainActivity"
        android:id="@+id/constraintLayout">
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/lettersToGuess"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginBottom="32dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal"
            app:layout_constraintBottom_toTopOf="@+id/theTableLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
    
            tools:context=".ui.mainActivity.MainActivity">
            <TextView
                android:id="@+id/product_name"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:padding="5dip"
                android:text=""
                android:textSize="16dip"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_marginLeft="5dp"
                android:background="@drawable/textlines"
                android:backgroundTint="@color/colorPrimary"
                xmlns:android="http://schemas.android.com/apk/res/android"
                />
        </LinearLayout>
    
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:id="@+id/theTableLayout">
    
            <TableRow
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:weightSum="7">
    
                <Button xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/buttonA"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
    
    
    
                    android:text="A"
                    android:theme="@style/AlphabetButton"
                    android:soundEffectsEnabled="false"
                    android:layout_weight="1"/>
    
                <Button xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/buttonB"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
    
                    android:text="B"
                    android:theme="@style/AlphabetButton"
                    android:soundEffectsEnabled="false"
                    android:layout_weight="1"/>
    
    
                <Button xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/buttonC"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
    
                    android:text="C"
                    android:theme="@style/AlphabetButton"
                    android:soundEffectsEnabled="false"
                    android:layout_weight="1"/>
    
                <Button xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/buttonD"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
    
                    android:text="D"
                    android:theme="@style/AlphabetButton"
                    android:soundEffectsEnabled="false"
                    android:layout_weight="1"/>
    
    
                <Button xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/buttonE"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
    
                    android:text="E"
                    android:theme="@style/AlphabetButton"
                    android:soundEffectsEnabled="false"
                    android:layout_weight="1"/>
    
                <Button xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/buttonF"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
    
                    android:text="F"
                    android:theme="@style/AlphabetButton"
                    android:soundEffectsEnabled="false"
                    android:layout_weight="1"/>
    
    
                <Button xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/buttonG"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
    
                    android:text="G"
                    android:theme="@style/AlphabetButton"
                    android:soundEffectsEnabled="false"
                    android:layout_weight="1"/>
    
            </TableRow>
    
        </TableLayout>
    </android.support.constraint.ConstraintLayout>
    

    文本行。xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#000000" />
            </shape>
        </item>
        <item android:bottom="3dp" >
            <shape android:shape="rectangle" >
                <solid android:color="#ffffff"/>
            </shape>
        </item>
    
    </layer-list>
    

    样式。xml

        <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    
        <style name="AlphabetButton" parent="Theme.AppCompat.Light">
            <item name="colorControlHighlight">@color/colorAccent</item>
        </style>
    
    </resources>
    

    guessing\u letter\u text\u视图。xml

    <TextView
    android:id="@+id/product_name"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:padding="5dip"
    android:text=""
    android:textSize="16dip"
    android:textStyle="bold"
    android:gravity="center"
    android:layout_marginLeft="5dp"
    android:background="@drawable/textlines"
    xmlns:android="http://schemas.android.com/apk/res/android" />
    

    正如你所看到的,点击了“D”按钮,调整了填充,这不是我想要的。更奇怪的是,如果在没有超时的情况下添加视图,我的问题就会消失。有人知道一种在超时后添加视图的方法,但不让它们弄乱其他视图的填充吗?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   Cristiano Tenuta    8 年前

    由于某些未知的原因,当您以编程方式延迟添加视图时,约束布局会变得混乱。

    因此,您必须在延迟后“重新应用”。

    过来看:

    public class MainActivity extends AppCompatActivity {
    
       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.constraintLayout);
            final ConstraintSet set = new ConstraintSet();
            set.clone(layout); //clone the layout first
    
    
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    LinearLayout linearLayout = findViewById(R.id.lettersToGuess);
    
                    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    TextView asdfsdfanotherTV = (TextView) layoutInflater.inflate(R.layout.guessing_letter_text_view, linearLayout, false);
    
                    linearLayout.addView(asdfsdfanotherTV);
    
    
                   set.applyTo(layout); //reapply the cloned layout
                }
            }, 1000);
        }
    }
    

    我已经在我的设备上进行了测试,它可以正常工作。看看它是否也对你有用。

    希望这有帮助。