以下screenshor是我的項目的一部分,灰色列表我使用listview而不是popupwindow
。 但我想要實現像popupwindow
這樣的效果,當我點擊popupwindow的外部部分時,彈出窗口就會消失。 我怎樣才能點擊小部件的外部和小部件解僱?
我能爲做,請教我,謝謝先進
以下screenshor是我的項目的一部分,灰色列表我使用listview而不是popupwindow
。 但我想要實現像popupwindow
這樣的效果,當我點擊popupwindow的外部部分時,彈出窗口就會消失。 我怎樣才能點擊小部件的外部和小部件解僱?
我能爲做,請教我,謝謝先進
如果使容器爲紅色區域佈局,如LinearLayout
或RelativeLayout
填滿屏幕,然後你可以把它點擊通過XML或以編程方式捕獲點擊。 Here是如何做到這一點的快速例子。
這假設你只是想解僱,如果白色區域被點擊。
更新:下面是一個簡單的例子。這個小應用程序會將白色區域設置爲紅色,如果它被點擊。在點擊監聽器中,您可以輕鬆地完成您需要的任務來消除紅色區域。
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<!--Set onClick and clickable here to capture clicks. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/backgroundLayout"
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:background="@android:color/white"
android:clickable="true"
android:onClick="layoutClicked"
tools:context="com.example.layout2.MainActivity">
<!--Set clickable here, too, to capture clicks so they don't propagate
to underlying view. The button is still enabled, though. -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_blue_bright"
android:clickable="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</LinearLayout>
而且支持代碼:
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void layoutClicked(View view) {
// Set the background color of the "outside" area to red.
// This is where you would dismiss the red area.
view.setBackgroundColor(0xFFDD0000);
}
}
你想,當用戶點擊下方的紅色標記的區域顯示一個彈出
你的意思是添加可點擊紅色區域父組?我添加,它不wor,我想單擊白色部分和紅色區域dismiss –
@Jsonzhang查看更新的答案。 – Cheticamp
我嘗試了,只有我點擊頂部,紅色區域纔會消失,並且我點擊紅色區域的後面,它不會消除 –
。 – Aditya
不,我想知道如何使紅色解僱,當我點擊紅色的外部部分 –