android - 尝试布局叠加层的透明背景

时间:2013-11-15 09:09:56

标签: android xml android-layout listview

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

这是弹出窗口的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 个答案:

答案 0 :(得分:0)

更改

android:background="#00000000"

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

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

中删除FrameLayout

答案 1 :(得分:0)

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

android:background="@drawable/transparent_image"

这对我有用..

答案 2 :(得分:0)

使用这个woking为我透明背景....

  framelayout.setBackgroundColor(Color.TRANSPARENT);

答案 3 :(得分: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" />