Lollipop或Marshmallow设备上没有显示以编程方式添加的高程

时间:2017-01-26 22:45:40

标签: android view fragment android-linearlayout android-elevation

我正在尝试向LinearLayout(片段中)添加高程。在我使用Android Nougat的设备上一切都很好,但在Marshmellow或Lollipop上根本没有阴影。可能是什么问题?

LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.main_ll_newproducts);
    ll.setBackgroundColor(Color.rgb(50,50,50));

        LinearLayout llProduct = new LinearLayout(getContext());
        llProduct.setBackgroundResource(R.drawable.product_selection_background);
        llProduct.setOrientation(LinearLayout.HORIZONTAL);
        llProduct.setPadding(20,0,0,0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            llProduct.setElevation(8);
        }
        llProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        //Layoutparams
        LayoutParams layParam = (LayoutParams) ll.getLayoutParams();
        layParam.setMargins(10,10,10,30);
        layParam.width = LayoutParams.MATCH_PARENT;
        layParam.height = LayoutParams.WRAP_CONTENT;
        // /Layoutparams

        llProduct.setLayoutParams(layParam);

之后我添加了一些像Textviews和一个ImageView的视图,然后我将这个LinearLayout添加到它的父级。

ll.addView(llProduct);

1 个答案:

答案 0 :(得分:0)

实际上,LayoutParams就是问题所在。我把它换成了:

//Layoutparams
        LinearLayout.LayoutParams layParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        layParam.setMargins(20,10,20,30);
        layParam.width = FrameLayout.LayoutParams.MATCH_PARENT;
        layParam.height = FrameLayout.LayoutParams.WRAP_CONTENT;
        llProduct.setLayoutParams(layParam);
        // /Layoutparams