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

时间:2018-02-28 23:01:21

标签: android android-layout timeout delay padding

我遇到了一个问题,当我在延迟后添加视图时,当我点击它们时会更改字母按钮上的填充。

MainActivity:

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

textlines.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>

styles.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_letter_text_view.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" />

正如您所看到的,&#39; D&#39;单击按钮并调整了填充,这不是我想要发生的。让我更加奇怪的是,如果我在没有超时的情况下添加视图,我的问题就会消失。有没有人知道在超时后添加视图的方法,但没有让它们搞砸其他视图的填充?

由于

1 个答案:

答案 0 :(得分:1)

由于某些未知原因,当您以编程方式添加视图时会出现延迟,因此约束布局会搞乱。

所以你必须在延迟之后“重新申请”它。

检查出来:

     <div id="quick-book">
        <script>
        function checkRadio(radio) {
           if (radio.id === "radio-restaurant"){
                document.getElementById("button-div").innerHTML = "<a href='./restaurant.html'>Submit</a>";
           } else {
                document.getElementById("button-div").innerHTML = "<a href='./hotel.html'>Submit</a>";
           }
          }
        </script>
        <form name="frmRadio" id="radio-buttons" action="">
            <input type="radio" id="radio-restaurant" name="option" onchange="checkRadio(this)">Restaurant<br>
            <input type="radio" id="radio-hotel" name="option" onchange="checkRadio(this)">Hotel<br>
        </form>
        <div id="button-div">
        </div>
    </div>

我已经在我的设备上测试了它的确有效。看看它是否适合你。

希望这有帮助。