在ImageView上覆盖具有不透明度的视图

时间:2014-10-13 17:38:26

标签: android xml android-layout

目前我有一个布局xml文件,我绑定了很多视图。我在这里遇到了一个问题。问题是我无法在(心脏图像)Imageview 上叠加视图。在布局 xml 文件中,我可以设置,但我无法在 imageview 上叠加视图。请帮我计算一下。我想要黑色Heart Imageview上的不透明度视图。

Currently the problem of lying view on Imageview with Opacity

这是xmlsource代码。我用*标记了重要的代码。              

    <LinearLayout
        android:id="@+id/listlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="top"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/datalist"
            style="@style/listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/layout2"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#F18EA4"
        android:gravity="bottom" >

        <TextView
            android:id="@+id/income_entry_container"
            style="@style/LightYellow"
            android:layout_width="70dp"
            android:layout_height="20dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:gravity="left|center"
            android:text="@string/confirm_income" />

        <TextView
            android:id="@+id/expense"
            style="@style/LightYellow"
            android:layout_width="70dp"
            android:layout_height="20dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/income_entry_container"
            android:gravity="left|center"
            android:text="@string/confirm_expense" />

        <TextView
            android:id="@+id/confirm_income_item_TextView"
            style="@style/LightYellow"
            android:layout_width="70dp"
            android:layout_height="20dp"
            android:layout_above="@+id/expense"
            android:layout_toRightOf="@+id/income_entry_container"
            android:gravity="right|center"
            android:text="@string/confrim_income_item" />

        <TextView
            android:id="@+id/confirm_expense_item_TextView"
            style="@style/LightYellow"
            android:layout_width="70dp"
            android:layout_height="20dp"
            android:layout_alignLeft="@+id/confirm_income_item_TextView"
            android:layout_below="@+id/confirm_income_item_TextView"
            android:gravity="right|center"
            android:text="@string/confirm_expense_item" />

        <TextView
            android:id="@+id/confirm_income_amount_TextView"
            style="@style/LightYellow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:gravity="right|center"
            android:text="@string/confirm_income_amount" />

        <TextView
            android:id="@+id/confrim_expnese_amount_TextView"
            style="@style/LightYellow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/confirm_income_item_TextView"
            android:gravity="right|center"
            android:text="@string/confirm_expnese_amount" />

        <ImageButton
            android:id="@+id/up_down_imageButton"
            style="@style/UpDownButtonSelector"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="170dp"
            android:layout_marginTop="10dp"
            android:contentDescription="@string/up_down_imageButton" />

        <TextView
            android:id="@+id/save_percentage"
            style="@style/LightYellow"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_below="@+id/balance_amount_TextView"
            android:layout_toRightOf="@+id/heart_ImageView"
            android:gravity="left|center"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/lbl_balance"
            style="@style/LightYellow"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/confrim_expnese_amount_TextView"
            android:gravity="left|center"
            android:text="@string/usable_amount"
            android:textSize="12sp" />

        **<ImageView
            android:id="@+id/heart_ImageView"
            style="@style/LightYellow"
            android:layout_width="260dp"
            android:layout_height="20dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/lbl_balance"
            android:contentDescription="@string/heart_content" />**

        <TextView
            android:id="@+id/balance_amount_TextView"
            style="@style/LightYellow"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_alignBaseline="@+id/lbl_balance_amount"
            android:layout_alignBottom="@+id/lbl_balance_amount"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/lbl_balance_amount"
            android:gravity="right|center"
            android:text="@string/balance_amount"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/lbl_balance_amount"
            style="@style/LightYellow"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_below="@+id/confrim_expnese_amount_TextView"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/lbl_balance"
            android:gravity="right|center"
            android:text="@string/lbl_balance_amount"
            android:textSize="14sp" />

        **<View
            android:id="@+id/overlay"
            android:layout_width="260dp"
            android:layout_height="20dp"
            android:layout_below="@+id/lbl_balance" />**

    </RelativeLayout>

</RelativeLayout>

这是源代码。我用*。

标记了重要的代码
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_buta_confirm, container, false);

        listRelativeLayout = (RelativeLayout) v
                .findViewById(R.id.fragment_list);
        int top = ((ButaColleTabBarActivity) getActivity())
                .getActionBarHeight() + 2;
        listRelativeLayout.setPadding(0, top, 0, 120);

        listLinearLayout = (LinearLayout) v.findViewById(R.id.listlayout);
        listLinearLayout.setPadding(0, 0, 0, 160);

        ie_adapter = new Income_Expense_Adapter(getActivity(),
                android.R.layout.simple_list_item_1, butaColleList);

        view = (ListView) v.findViewById(R.id.datalist);
        view.setAdapter(ie_adapter);
        view.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                ButaColle butaColle = butaColleList.get(position);
                FragmentManager fragmentManager = getFragmentManager();
                ButaColleDetailIncomeExpenseFragment butaDetail = ButaColleDetailIncomeExpenseFragment
                        .newInstance(butaColle);

                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.replace(R.id.realTabContent, butaDetail);
                fragmentTransaction.addToBackStack("SearchFragment");
                fragmentTransaction.commit();
            }
        });

        view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        view.setMultiChoiceModeListener(new MultiChoiceModeListener() {

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                ie_adapter.removeSelection();
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.menu_delete_context, menu);
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch (item.getItemId()) {
                case R.id.menu_item_delete_data:
                    SparseBooleanArray selected = ie_adapter.getSelectedIds();
                    for (int i = (selected.size() - 1); i >= 0; i--) {
                        if (selected.valueAt(i)) {
                            ButaColle butaitem = ie_adapter.getItem(selected
                                    .keyAt(i));
                            dbhelper.deleteHistoryRecords(butaitem);
                            ie_adapter.remove(butaitem);
                        }
                    }
                    mode.finish();
                    return true;
                default:
                    return false;
                }
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode,
                    int position, long id, boolean checked) {
                final int checkedCount = view.getCheckedItemCount();
                mode.setTitle(checkedCount + " Selected");
                ie_adapter.toggleSelection(position);
            }
        });

        updownImageButton = (ImageButton) v
                .findViewById(R.id.up_down_imageButton);
        updownImageButton.setBackgroundResource(R.drawable.close01);
        updownImageButton.setTag(R.drawable.close01);
        updownImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (updownImageButton.getTag().equals((R.drawable.close01))) {
                    updownImageButton.setBackgroundResource(R.drawable.open01);
                    updownImageButton.setTag(R.drawable.open01);
                    int top = ((ButaColleTabBarActivity) getActivity())
                            .getActionBarHeight() + 2;
                    listRelativeLayout.setPadding(0, top, 0, 50);
                    listLinearLayout.setPadding(0, 0, 0, 160);
                } else if (updownImageButton.getTag().equals(R.drawable.open01)) {
                    updownImageButton.setBackgroundResource(R.drawable.close01);
                    updownImageButton.setTag(R.drawable.close01);
                    int top = ((ButaColleTabBarActivity) getActivity())
                            .getActionBarHeight() + 2;
                    listRelativeLayout.setPadding(0, top, 0, 120);
                    listLinearLayout.setPadding(0, 0, 0, 160);
                }
            }

        });

        int itemCount = 0;
        String itemlbl = "item";
        incomeItemcount_TextView = (TextView) v
                .findViewById(R.id.confirm_income_item_TextView);
        itemCount = dbhelper.getItemCount(1);
        if (itemCount > 1)
            itemlbl = "items";
        incomeItemcount_TextView.setText(String.valueOf(itemCount) + itemlbl);

        itemCount = 0;
        itemlbl = "item";
        expenseItemcount_TextView = (TextView) v
                .findViewById(R.id.confirm_expense_item_TextView);
        itemCount = dbhelper.getItemCount(0);
        if (itemCount > 1)
            itemlbl = "items";
        expenseItemcount_TextView.setText(String.valueOf(itemCount) + itemlbl);

        double balance = 0.0;
        double incomeAmount = 0.0;
        double expenseAmount = 0.0;
        incomeAmount_TextView = (TextView) v
                .findViewById(R.id.confirm_income_amount_TextView);
        incomeAmount = dbhelper.getTotalAmount(1);
        incomeAmount_TextView.setText(String.valueOf(incomeAmount) + "$");

        expenseAmount_TextView = (TextView) v
                .findViewById(R.id.confrim_expnese_amount_TextView);
        expenseAmount = dbhelper.getTotalAmount(0);
        expenseAmount_TextView.setText(String.valueOf(expenseAmount) + "$");

        balance = incomeAmount - expenseAmount;
        balance_TextView = (TextView) v
                .findViewById(R.id.balance_amount_TextView);
        balance_TextView.setText(String.valueOf(balance) + "$");

        int percentage = 0;
        percentage = (int) ((balance / incomeAmount) * 100);
        if (balance > 0) {
            heartImageView = (ImageView) v.findViewById(R.id.heart_ImageView);
            heartImageView.setImageResource(R.drawable.graph);
            percentage_TextView = (TextView) v
                    .findViewById(R.id.save_percentage);
            percentage_TextView.setText(String.valueOf(percentage) + "%");
        } else {
            heartImageView = (ImageView) v.findViewById(R.id.heart_ImageView);
            heartImageView.setImageResource(0);
            percentage_TextView = (TextView) v.findViewById(0);
            percentage = 0;
        }
        **View overlay = (View) v.findViewById(R.id.overlay);
        int opacity = 200; // from 0 to 255
        overlay.setBackgroundColor(opacity * 0x1000000); // black with a variable alpha
        RelativeLayout.LayoutParams params =
            new RelativeLayout.LayoutParams(300, 36);
        overlay.setLayoutParams(params);
        overlay.invalidate();**
        return v;
    }

请帮我看看。感谢你!

2 个答案:

答案 0 :(得分:0)

您可以尝试使用addRule方法

向params添加规则
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

我不在我的电脑附近进行测试,但您可以使用addRule方法设置lbl_balance下方的视图

或者,尝试一起删除代码中的布局参数。 xml使视图膨胀,因此它已经创建。您在代码中需要做的就是设置背景颜色。您也可以使用

在xml中执行此操作
android:background="#80000000"

答案 1 :(得分:0)

尝试更改

**<View
        android:id="@+id/overlay"
        android:layout_width="260dp"
        android:layout_height="20dp"
        android:layout_below="@+id/lbl_balance" />**

 **<View
            android:id="@+id/overlay"
            android:layout_width="260dp"
            android:layout_height="20dp"
            android:layout_alignBottom="@+id/heart_ImageView" />** 

所做的更改:布局对齐以及对齐的ID