【问题标题】:Android Layout - Disable unnecessary areaAndroid 布局 - 禁用不必要的区域
【发布时间】:2019-02-12 01:54:39
【问题描述】:

我正在为退出确认创建一个自定义弹出窗口。为此,我使用RelativeLayoutlayout_width="match_parent"layout_height="match_parent" 属性。

如何使WebView 区域无法滚动和点击?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/content_main"
    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"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/banner_main">

            <WebView
                android:id="@+id/web"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:minHeight="400dp" />

    </ScrollView>

    <RelativeLayout
        android:id="@+id/exit_popup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="0dp"
        android:orientation="vertical"
        android:visibility="gone"
        android:background="#80111111"
        android:padding="0dp">

        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:background="#fff"
            app:cardUseCompatPadding="true"
            app:cardElevation="10dp"
            app:cardCornerRadius="0dp"
            ads:contentPadding="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:padding="5dp"
                    android:text="ARE YOU SURE TO EXIT?"
                    android:textColor="#000"
                    android:textSize="18dp"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:paddingBottom="0dp"
                    android:paddingLeft="0dp"
                    android:paddingRight="0dp"
                    android:paddingTop="7dp">

                    <Button
                        android:id="@+id/button_cancel"
                        android:drawableLeft="@drawable/ic_back"
                        style="@style/Widget.AppCompat.Button.Colored"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="18dp"
                        android:layout_marginRight="7dp"
                        android:layout_weight="1"
                        android:text="Nope"
                        android:textColor="#fff"
                        android:textSize="14sp" />

                    <Button
                        android:id="@+id/button_exit"
                        android:drawableRight="@drawable/ic_arrow_forward"
                        style="@style/Widget.AppCompat.Button.Colored"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="18dp"
                        android:layout_weight="1"
                        android:text="Yes"
                        android:textColor="#fff"
                        android:textSize="14sp" />

                </LinearLayout>
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </RelativeLayout>
</RelativeLayout>

【问题讨论】:

  • 为什么不直接使用不可取消的 AlertDialog?
  • @TheWanderer 我想用布局创建更多自定义样式。
  • 您可以使用 AlertDialogs 做到这一点。只需使用setView()
  • 如果你想要那个。显示对话框时只需禁用scrollview.setEnabled(false)

标签: java android xml android-layout


【解决方案1】:

这是我在 webview 中禁用所有滚动的代码:

disable scroll on touch
webview.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    return (event.getAction() == MotionEvent.ACTION_MOVE);
  }
});

要仅隐藏滚动条,请在执行弹出窗口时添加代码。

 web = (WebView) findViewById(R.id. web); 
 web.setVerticalScrollBarEnabled(false);
 web.setHorizontalScrollBarEnabled(false);

你也可以尝试用垂直滚动的滚动视图包裹你的 webview 并禁用 webview 上的所有滚动:

web.setScrollContainer(false);

【讨论】:

    【解决方案2】:

    通过在背景布局上添加 onClick 侦听器解决问题。

    <RelativeLayout
            android:id="@+id/exit_popup"
            android:onClick="nullMethod"
    
    public void nullMethod(View view) {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      相关资源
      最近更新 更多