【问题标题】:Programmatically added elevation is not showing on Lollipop or Marshmallow devicesLollipop 或 Marshmallow 设备上未显示以编程方式添加的海拔高度
【发布时间】:2017-06-12 13:34:46
【问题描述】:

我正在尝试将高程添加到 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);

【问题讨论】:

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


    【解决方案1】:

    实际上 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-26
      • 2015-01-15
      • 2014-12-25
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-05
      相关资源
      最近更新 更多