一个ViewSwitcher内CoordinatorLayout / RelativeLayout的问题 [英] CoordinatorLayout / RelativeLayout issue within a ViewSwitcher

本文介绍了一个ViewSwitcher内CoordinatorLayout / RelativeLayout的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这其中的一个跟进的问题:<一href=\"http://stackoverflow.com/questions/34820709/coordinator-layout-and-relative-layout-issue\">Coordinator布局和相对布局问题

我有一个问题,当你有一个 CoordinatorLayout RelativeLayout的中的 ViewSwitcher 像下面的例子:

 &LT;?XML版本=1.0编码=UTF-8&GT?;&LT; ViewSwitcher
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:ID =@ + ID /切换&GT;    &LT;! - 首先切换视图/开机画面 - &GT;
    &LT; RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:背景=@色/ colorAccent&GT;
    &LT; / RelativeLayout的&GT;    &LT;! - 二切换视图 - &GT;
    &LT; android.support.design.widget.CoordinatorLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
        的xmlns:工具=htt​​p://schemas.android.com/tool​​s
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:fitsSystemWindows =真
        工具:上下文=com.example.test&GT;        &LT; android.support.design.widget.AppBarLayout
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:主题=@风格/ AppTheme.AppBarOverlay&GT;            &LT; android.support.v7.widget.Toolbar
                机器人:ID =@ + ID /工具栏
                机器人:layout_width =match_parent
                机器人:layout_height =?ATTR / actionBarSize
                机器人:ATTR / colorPrimary背景=
                应用:popupTheme =@风格/ AppTheme.PopupOverlay/&GT;        &LT; /android.support.design.widget.AppBarLayout>
        &LT;! - 如@ LinX64在previous问题建议 - &GT;
        &LT; RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT&GT;            &LT;按钮
                机器人:ID =@ + ID /按钮
                机器人:layout_width =match_parent
                机器人:layout_height =70dp
                机器人:layout_alignParentBottom =真
                机器人:背景=@彩色/ colorAccent
                机器人:可聚焦=真
                机器人:focusableInTouchMode =真
                机器人:文本=TEXT
                机器人:文字颜色=@机器人:彩色/白/&GT;        &LT; / RelativeLayout的&GT;
        &LT; android.support.design.widget.FloatingActionButton
            机器人:ID =@ + ID / FAB
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =底部|结束
            机器人:layout_margin =@扪/ fab_margin
            机器人:SRC =@机器人:可绘制/ ic_dialog_email/&GT;    &LT; /android.support.design.widget.CoordinatorLayout>
&LT; / ViewSwitcher&GT;

这在我的活动

 最后ViewSwitcher切换=(ViewSwitcher)findViewById(R.id.switcher);//如果我使用switcher.showNext()一旦活动开始,一切工作正常。新CountDownTimer(1500,1500){
   @覆盖
   公共无效onTick(长millisUntilFinished){}   @覆盖
   公共无效onFinish(){
       switcher.showNext();
   }
}。开始();

结果: Moto G的 的Andr​​oid 5.1采取截图) >

正如你所看到的,按钮是不是在屏幕的底部,它被裁剪。

预期结果:

在这里输入的形象描述

清单:

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;表明包=com.example.test
          的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;    &lt;应用
        机器人:allowBackup =真
        机器人:图标=@的mipmap / ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:supportsRtl =真
        机器人:主题=@风格/ AppTheme&GT;
        &LT;活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@风格/ AppTheme.NoActionBar&GT;
            &所述;意图滤光器&gt;
                &lt;作用机器人:名字=android.intent.action.MAIN/&GT;                &LT;类机器人:名字=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;
    &LT; /用途&gt;&LT; /清单&GT;

样式

 &LT;资源&GT;    &LT;! -  Base应用程序的主题。 - &GT;
    &LT;样式名称=AppTheme父=Theme.AppCompat.Light.DarkActionBar&GT;
        &LT;! - 在这里自定义的主题。 - &GT;
        &LT;项目名称=colorPrimary&GT; @彩色/ colorPrimary&LT; /项目&GT;
        &LT;项目名称=colorPrimaryDark&GT; @彩色/ colorPrimaryDark&LT; /项目&GT;
        &LT;项目名称=colorAccent&GT; @彩色/ colorAccent&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=AppTheme.NoActionBar&GT;
        &LT;项目名称=windowActionBar&GT;假LT; /项目&GT;
        &LT;项目名称=windowNoTitle&GT;真&LT; /项目&GT;
    &LT; /风格&GT;    &LT;样式名称=AppTheme.AppBarOverlay父=ThemeOverlay.AppCompat.Dark.ActionBar/&GT;    &LT;样式名称=AppTheme.PopupOverlay父=ThemeOverlay.AppCompat.Light/&GT;&LT; /资源&GT;


解决方案

为我工作与 runOnUiThread(新的Runnable() 工作后,它

 公共类MainActivity扩展AppCompatActivity {    公共处理器mHandler;
    私人ViewSwitcher切换;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        工具条工具栏=(栏)findViewById(R.id.toolbar);
        setSupportActionBar(工具栏);        切换=(ViewSwitcher)findViewById(R.id.switcher);
        mHandler =新的处理程序();        runOnUiThread(新的Runnable(){            @覆盖
            公共无效的run(){
                // TODO自动生成方法存根
                尝试{
                    视频下载(5000); //每5秒
                    mHandler.post(新的Runnable(){                        @覆盖
                        公共无效的run(){
                            // TODO自动生成方法存根
                            switcher.showNext();
                        }
                    });
                }赶上(例外五){
                    // TODO:处理异常
                }
            }
        });    }
}

当然, XML

 &LT;! - 为@ LinX64建议在previous的问题 - &GT;
        &LT; RelativeLayout的
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT&GT;            &LT;按钮
                机器人:ID =@ + ID /按钮
                机器人:layout_width =match_parent
                机器人:layout_height =70dp
                机器人:layout_alignParentBottom =真
                机器人:背景=@彩色/ colorAccent
                机器人:可聚焦=真
                机器人:focusableInTouchMode =真
                机器人:文本=TEXT
                机器人:文字颜色=@机器人:彩色/白/&GT;        &LT; / RelativeLayout的&GT;

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>

解决方案

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>

这篇关于一个ViewSwitcher内CoordinatorLayout / RelativeLayout的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆