【问题标题】:Adding views after timeout causes padding on other views to go awry - Android超时后添加视图会导致其他视图上的填充出错 - Android
【发布时间】:2018-11-30 15:11:19
【问题描述】:

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

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" />

如您所见,“D”按钮已被单击,并且填充已被调整,这不是我想要发生的。更奇怪的是,如果我在没有超时的情况下添加了视图,我的问题就会消失。有谁知道在超时后添加视图但又不会弄乱其他视图的填充的方法?

谢谢

【问题讨论】:

  • 能否发布您的 styles.xml 文件,以便我可以尝试在此处重现问题?
  • 嗨,已经添加了styles.xml :)
  • 对不起...我还需要guessing_letter_text_view 布局。
  • 已添加:)

标签: android android-layout timeout delay padding


【解决方案1】:

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

所以你必须在延迟后“重新应用”它。

检查一下:

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);
    }
}

我已经在我的设备上进行了测试,它可以工作。看看它是否也适合你。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2015-05-26
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多