【发布时间】:2016-01-26 19:50:31
【问题描述】:
我在CoordinatorLayout 内有一个WebView,在WebView 下方有一个Button,但按钮永远不会出现。我也试过LinearLayout和FrameLayout。
我尝试使用NestedScrollView 而不是RelativeLayout 并且遇到了同样的问题,即使我可以让它与NestedScrollView 一起使用,我也不能使用NestedScrollView 因为其他issues 它有一个WebView。
那么我该怎么做才能让 Button 显示在 WebView 下方而无需任何滚动?
这是我的布局:
<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.webviewwithbutton.WebViewWithButton">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"></WebView>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/webview"
android:text="test"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
这是我的java代码:
public class WebViewWithButton extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view_with_button);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
WebView view = (WebView) findViewById(R.id.webview);
view.setWebViewClient(new WebViewClient());
WebSettings settings = view.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setAppCacheEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setBuiltInZoomControls(true);
settings.setAppCacheEnabled(true);
settings.setAppCachePath(getCacheDir().getAbsolutePath());
settings.setDatabaseEnabled(true);
settings.setSupportMultipleWindows(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setDomStorageEnabled(true);
settings.setAllowContentAccess(true);
settings.setAllowFileAccess(true);
settings.setSaveFormData(true);
view.loadUrl("http://vimeo.com");
}
}
编辑:澄清一下,我想使用CoordinatorLayout。我知道我可以轻松完成此任务,问题仅在于使用CoordinatorLayout 时。
编辑:我的维度文件:
<resources>
<dimen name="app_bar_height">180dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="text_margin">16dp</dimen>
</resources>
编辑: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="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>
编辑:我应该提一下,我所做的只是使用名为 Scrolling Activity 的 Android Studio 模板之一。
【问题讨论】:
-
为什么不直接使用 LinearLayout 而不是 RelativeLayout?
-
我也试过了,同样的问题。
-
@mntgoat -
FrameLayout怎么样?我做到了,它正在工作,看看我的答案。 -
@LinX64 是的,我也试过了。
-
您测试了哪些 Android 版本的代码?
标签: android webview android-webview android-support-library android-coordinatorlayout