Android ViewSwitcher中的协调布局/相对布局问题
这是这个问题的后续问题: 当您在Android ViewSwitcher中的协调布局/相对布局问题,android,android-layout,android-relativelayout,android-coordinatorlayout,viewswitcher,Android,Android Layout,Android Relativelayout,Android Coordinatorlayout,Viewswitcher,这是这个问题的后续问题: 当您在视图切换器中有一个带有RelativeLayout的协调布局时,我遇到了一个问题,如以下示例所示: <?xml version="1.0" encoding="utf-8"?> <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layo
视图切换器
中有一个带有RelativeLayout的协调布局
时,我遇到了一个问题,如以下示例所示:
<?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>
结果:(截图取自Moto G
Android
5.1)
正如您所看到的,按钮不在屏幕底部,它被裁剪了
预期结果:
<?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>
清单:
<?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>
风格
<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>
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
假的
真的
为我工作后使用了rununuithread(new Runnable()
)
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
}
}
});
}
}
当然,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>
为我工作后使用了rununuithread(new Runnable()
)
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
}
}
});
}
}
当然,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>
它在真实设备上工作:)截图取自真实设备,Moto G 4G,Android 5.1:(怪异:4.4.2:同样在摩托罗拉MC40 Android 4(记不清确切版本)中,你真的应该使用a作为启动屏幕,完全跳过人为延迟和视图切换。它在真实设备上工作:)屏幕截图取自真实设备Moto G 4G,Android 5.1:(奇怪:4.4.2:同样在摩托罗拉MC40 Android 4(记不清确切版本)中,你真的应该使用a作为启动屏幕,完全跳过人为延迟和视图切换器
。举个例子,视图切换器的第一个视图没有显示(启动屏幕),全部空白,5秒钟后,随着一个微小的闪烁,我看到了视图(按钮在底部)是的,但我想这个问题解决了,尝试修复飞溅。如果我看不到视图切换器的第一个视图,问题就没有解决,因为根本没有使用视图切换器,我只看到第二个视图。预期结果:闪屏-延迟(5秒)-活动主布局。在您的示例中:空白屏幕-延迟(5秒)-主布局。这就是为什么我这么做:)我会使用postDelayed()切换处理程序。使用您的示例,视图切换器的第一个视图没有显示(初始屏幕),所有视图都是空白的,5秒后,我看到视图中有一点闪烁(按钮在底部)是的,但我想这个问题解决了,我也在尝试修复飞溅。如果我看不到视图切换器的第一个视图,问题就没有解决,因为根本没有使用视图切换器,我只看到第二个视图。预期结果:飞溅屏幕-延迟(5秒)-活动主布局。以您的示例:空白屏幕-延迟(5秒)-主布局。这就是为什么我这么做:)我会使用postdayed()切换处理程序。