Android LinearLayout整个布局设置不可点击

1,692 阅读1分钟

解决办法:

在代码中给布局(例如LinearLayout)设置 setOnClickListener(null);

要的效果:

下图中的Popoupwindow布局中的LinearLayout不可点击

 上图效果的代码实现:

1,activity的xml布局(布局中有个Button按钮,点击按钮弹出一个popupwindow )

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ic_launcher_background"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:onClick="click"
        android:textSize="30sp"
        android:text="弹窗Popupwindow"/>

</LinearLayout>

2,上图中有白色背景的popupwindow 的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:id="@+id/ll_popup">

    <LinearLayout
        android:id="@+id/ll_popup_content"
        android:layout_width="600dp"
        android:layout_height="400dp"
        android:gravity="center"
        android:background="@android:color/white">
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="250dp"
            android:padding="10dp"
            android:src="@drawable/fengjing"
            android:layout_marginRight="20dp"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="250dp"
            android:padding="10dp"
            android:src="@drawable/small"/>
    </LinearLayout>

</LinearLayout>

3,MainActivity的代码实现:

public class MainActivity extends AppCompatActivity {
    private View pop;

    private LinearLayout ll_popup,ll_popup_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pop = View.inflate(this, R.layout.popupwindow_test, null);
        ll_popup = pop.findViewById(R.id.ll_popup);
        ll_popup_content = pop.findViewById(R.id.ll_popup_content);



        //Linearloyout就不可点击了
        ll_popup_content.setOnClickListener(null);
    }

    public void click(View view) {
       final PopupWindow popupWindow = new PopupWindow(pop, MATCH_PARENT, MATCH_PARENT);
        ll_popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.TOP,0,0);
    }




    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        //完全沉浸式
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}

相关知识:

其他方法,但是对于我的需求都没有效果

都是设置setClickable(false)或者setClickable(false)或者setFocusable(false)

setClickable():设置成true时,按钮为可点击,设置为false时,按钮不可点击,不能响应点击事件,但此时如果setEnabled()为true,那么按钮即使不可点击(setClickable()为false),也会产生变化(一闪一闪)。

setEnabled():设置成true时,相当于激活了按钮,按钮的状态不再是死的,而是会对触摸或者点击产生反应,并且可以响应一些触发事件。而设置成false时,按钮是灰色的,无论是否可点击(即使将setClickable()设置成true),都无法响应任何触发事件。
setFocusable:使能控件获得焦点,设置为true时,并不是说立刻获得焦点,要想立刻获得焦点,得用requestFocus;
使能获得焦点,就是说具备获得焦点的机会、能力,当有焦点在控件之间移动时,控件就有这个机会、能力得到焦点。