簡體   English   中英

透明/昏暗的ListView背景-單擊背景時我想破壞ListView活動

[英]Transparent/dim ListView background - I want to destroy ListView activity when clicking on background

我有一個運行良好的ListView列表。 它是這樣設置的:

<style name="Theme.Transparent" parent="android:Theme"> 
    <item name="android:windowIsTranslucent">true </item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>

這是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:padding="0dp" 
   android:background="@drawable/button_pressed"
   android:clickable="false"
   android:id="@+id/ListViewLayout" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp" />

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp" >
    </TextView>
</LinearLayout> 

我也有一段代碼來創建我的ListView並處理我的點擊:

public class RandomList extends ListActivity {
    static final String[] randomList = new String[] {"Name 1", "Name 2", "Name 3", "Logo"};

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new RandomArrayAdapter(this, randomList));
    }

    protected void onListItemClick(ListView l, View v, int position, long id){
        //Get selected items
        Log.v("ListView l:", l.toString());
        Log.v("View v:", v.toString());
        Log.v("Position:", ""+position);
        Log.v("ID: ", ""+id);
        String selectedValue = (String) getListAdapter().getItem(position);
        Bundle bundle = new Bundle();
        bundle.putString("Name", selectedValue);
        Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        finish();
    }
}

現在,有了ArrayAdapter等,發生的事情是我得到了一個項目列表。 由於只有五個項目,並且由於我為它設置的樣式,大部分屏幕都變暗了(原始Activity ),並且只有列表居中。 問題是,如果有人單擊列表框外部的灰色區域,我想完成Activity 我無法為自己的生活弄清楚如何做。

我試圖實現一個OnTouchListener ,但是ListView不允許我這樣做,而且我不知道如何為包圍TextViewLinearLayout容器調用它。 我是Android的新手,所以請幫我解決一下。

創建此xml布局,並將其命名為“ list_layout.xml”:

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

 <ListView android:id="@android:id/list"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>
 </LinearLayout>

然后將您的onCreate()更改為以下內容:

public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);

    View background = findViewById(R.id.my_background);
    background.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             RamdomList.this.finish();
         }
     });

    setListAdapter(new RandomArrayAdapter(this, randomList));

}

我沒有運行它就把它寫在了我的頭上,所以您可能會遇到一些麻煩,但是您明白了。

我找到了第二個問題的解決方案,詢問有關包裝盒的問題。 Theme.Transparent ,我只需要將WindowIsFloatingfalse 結合Wnafee的出色答案,它就像一個魅力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM