0

我正在尝试制作一个将出现在列表视图上的弹出窗口,具有透明背景,因此您仍然可以看到它后面的列表视图。

这是弹出窗口的xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00000000"
    android:gravity="center_horizontal|center_vertical" >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:background="@drawable/popup_bg"
        android:padding="20dip" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <!-- Left column -->

            <TextView
                android:id="@+id/foodName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/calories"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/foodName"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/fat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/calories"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/protein"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/fat"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/carbohydrates"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/protein"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:textSize="12sp" />
            <!-- Bottom -->

            <TextView
                android:id="@+id/sodium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/carbohydrates"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:textSize="12sp" />

            <Button
                android:id="@+id/btnAddFood"
                android:layout_width="200dip"
                android:layout_height="50dip"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/sodium"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:background="@drawable/popup_buttons"
                android:singleLine="true"
                android:text="Add this food" />
        </RelativeLayout>
    </FrameLayout>

</RelativeLayout>

这是调用它的活动(删除了一些不必要的行):

public class FoodPopupActivity extends Activity{
     String foodName;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        try{
        // receive food
         Bundle extras = getIntent().getExtras();
         final Food food = (Food) extras.get("food");
         String foodName = food.getName();


        // set view
        setContentView(R.layout.activity_food_popup);

        View background = findViewById(R.id.background);
        // background.setBackgroundColor(00000000);


        background.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1){
                finish();
                return false;
            }
        });


        addFood.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // pass an intent with user location and where they need to go
                // then start the intent
                MainActivity.myMeal.addFood(food);
                Toast.makeText(getApplicationContext(),food.getName()+" added to your menu for today.", Toast.LENGTH_SHORT).show();
                finish();
            }
        });



        }catch(Exception e){
            Toast.makeText(getApplicationContext(), "Error displaying food data, Try again, "+e.toString(), Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(),foodName+" "+String.valueOf(foodCalories), Toast.LENGTH_SHORT).show();

        }
    }

}

我相信我已经尝试在相对布局、线性布局和列表视图上加载它,但到目前为止没有成功。我究竟做错了什么?

4

4 回答 4

0

改变

android:background="#00000000"

android:background="@android:color/transparent"

另外,您需要android:background="@drawable/popup_bg"从您的FrameLayout

于 2013-11-15T09:14:28.270 回答
0

尝试将背景图像设置为透明图像。您可以轻松创建一个并将其保存为 png 文件。

android:background="@drawable/transparent_image"

这对我有用..

于 2013-11-15T12:40:14.313 回答
0

您可以将黑色设置为background颜色并使其透明,请尝试使用alpha属性:

<FrameLayout
    android:id="@+id/black_layer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:alpha="0.75" />
于 2019-08-21T22:16:51.153 回答
0

使用这个为我工作的透明背景....

  framelayout.setBackgroundColor(Color.TRANSPARENT);
于 2018-04-03T14:18:36.127 回答