CoordinatorLayout / RelativeLayout issue within a

2019-08-14 10:26发布

问题:

This is a follow up question of this one: Coordinator Layout and Relative Layout issue

I have an issue when you have a CoordinatorLayout with a RelativeLayout within a ViewSwitcher like the following example:

<?xml version="1.0" encoding="utf-8"?>

<ViewSwitcher  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/switcher">

    <!--First switcher view / Splash Screen-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent">
    </RelativeLayout>

    <!--Second switcher view-->
    <android.support.design.widget.CoordinatorLayout
        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:fitsSystemWindows="true"
        tools:context="com.example.test">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.AppBarLayout>


        <!--as @LinX64 suggested in previous question-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_alignParentBottom="true"
                android:background="@color/colorAccent"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:text="text"
                android:textColor="@android:color/white" />

        </RelativeLayout>


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email"/>

    </android.support.design.widget.CoordinatorLayout>
</ViewSwitcher>

And this in my Activity:

final ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.switcher);

//If I use switcher.showNext() as soon the activity starts, everything works fine.

new CountDownTimer(1500, 1500) {
   @Override
   public void onTick(long millisUntilFinished) {}

   @Override
   public void onFinish() {
       switcher.showNext();
   }
}.start();

Result: (Screenshot taken from Moto G Android 5.1)

As you can see, the button isn't at bottom of the screen and it's been cropped.

Expected result:

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.test"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Styles

<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="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

回答1:

Worked for me with runOnUiThread(new Runnable() after working on it

public class MainActivity extends AppCompatActivity {

    public Handler mHandler;
    private ViewSwitcher switcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        switcher = (ViewSwitcher) findViewById(R.id.switcher);
        mHandler = new Handler();

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    Thread.sleep(5000); // every 5 seconds
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            switcher.showNext();
                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        });

    }


}

And of course, XML:

<!--as @LinX64 suggested in previous question-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_alignParentBottom="true"
                android:background="@color/colorAccent"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:text="text"
                android:textColor="@android:color/white" />

        </RelativeLayout>