简体   繁体   English

透明/昏暗的ListView背景-单击背景时我想破坏ListView活动

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

I have a ListView list that operates nicely. 我有一个运行良好的ListView列表。 It's set this way: 它是这样设置的:

<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>

This is my xml file: 这是我的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> 

I also have a block of code which creates my ListView and handles my clicks: 我也有一段代码来创建我的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();
    }
}

Now, with the ArrayAdapter et al, what happens is I get a list of items. 现在,有了ArrayAdapter等,发生的事情是我得到了一个项目列表。 As there are only five items, and due to the style I set for it, most of the screen is dimmed out (the original Activity ), and only the list is centred. 由于只有五个项目,并且由于我为它设置的样式,大部分屏幕都变暗了(原始Activity ),并且只有列表居中。 The problem is, I want to finish the Activity should someone click on the gray region outside of the list box. 问题是,如果有人单击列表框外部的灰色区域,我想完成Activity I cannot for the life of me figure out how. 我无法为自己的生活弄清楚如何做。

I tried to implement an OnTouchListener , but ListView isn't letting me do so and I can't figure out how to call it for the LinearLayout container that surrounds the TextView . 我试图实现一个OnTouchListener ,但是ListView不允许我这样做,而且我不知道如何为包围TextViewLinearLayout容器调用它。 I'm fairly new to Android, so please dumb it down for me. 我是Android的新手,所以请帮我解决一下。

Create this xml layout and call it "list_layout.xml": 创建此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>

Then change your onCreate() to the following: 然后将您的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));

}

I wrote this off the top of my head without running it, so you might fight a few things off but you get the concept. 我没有运行它就把它写在了我的头上,所以您可能会遇到一些麻烦,但是您明白了。

I found the solution to my second question, asked regarding the box. 我找到了第二个问题的解决方案,询问有关包装盒的问题。 In the Theme.Transparent , I just had to turn WindowIsFloating to false . Theme.Transparent ,我只需要将WindowIsFloatingfalse Combine that with Wnafee's excellent answer and it worked like a charm. 结合Wnafee的出色答案,它就像一个魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM