java - setMargins 方法不适用于自定义 ViewGroup

标签 java android android-custom-view viewgroup

我正在开发一个项目,我的部分工作是在主屏幕上创建一个滑动布局,当应用程序启动时,该布局将部分显示在底部,我们可以滚动该布局。然后我必须在其中添加 subview ,这将是卡片。现在,我已经创建了一个自定义 ViewGroup,我将以编程方式添加它们。

我已经成功完成了任务的大部分部分。

我创建了一个用于滑动布局的自定义FrameLayout

然后我使用它并添加到 XML 中。

<?xml version="1.0" encoding="utf-8"?>
<com.dhrumil.airhockey.OverflowView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/layer_list"
android:padding="5dp"
android:layout_height="match_parent"/>

现在,我创建了一个名为 MyCardView 的 java 类,它扩展了 ViewGroup

public class MyCardView extends ViewGroup {

    public MyCardView(Context context){
        super(context);
        init(context);
    }

    public MyCardView(Context context, AttributeSet attrs){
        super(context,attrs);
        init(context);
    }

    public MyCardView(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs,defStyleAttr);
        init(context);
    }

    public MyCardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    public void init(Context context){

    }

    @Override
    protected void onLayout(boolean b, int l, int i1, int i2, int i3) {

        // TODO Auto-generated method stub
        final int count = getChildCount();
        int curWidth, curHeight, curLeft, curTop, maxHeight;

        //get the available size of child view
        int childLeft = this.getPaddingLeft();
        int childTop = this.getPaddingTop();
        int childRight = this.getMeasuredWidth() - this.getPaddingRight();
        int childBottom = this.getMeasuredHeight() - this.getPaddingBottom();
        int childWidth = childRight - childLeft;
        int childHeight = childBottom - childTop;

        maxHeight = 0;
        curLeft = childLeft;
        curTop = childTop;
        //walk through each child, and arrange it from left to right
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                //Get the maximum size of the child
                child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST),
                        MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST));
                curWidth = child.getMeasuredWidth();
                curHeight = child.getMeasuredHeight();
                //wrap is reach to the end
                if (curLeft + curWidth >= childRight) {
                    curLeft = childLeft;
                    curTop += maxHeight;
                    maxHeight = 0;
                }
                //do the layout
                child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight);
                //store the max height
                if (maxHeight < curHeight)
                    maxHeight = curHeight;
                curLeft += curWidth;
            }
        }

    }
}

然后我尝试以编程方式将 MyCardView 对象添加到我的自定义 FrameLayout 中。

这是执行任务的 onCreate() 内部的方法。即将两个自定义ViewGroup添加到一个LinearLayout中。

public void setupText(){
    FrameLayout frame = (FrameLayout) findViewById(R.id.overflow_frame);

    LinearLayout linearLayout = new LinearLayout(getBaseContext());
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    ViewGroup vg = new MyCardView(getBaseContext());
    ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,200);
    vg.setBackgroundColor(Color.WHITE);
    ViewCompat.setElevation(vg, 5);

    TextView tvTemp = new TextView(getBaseContext());
    tvTemp.setTextColor(Color.BLACK);
    tvTemp.setText("Hello There");

    vg.addView(tvTemp);

    ViewGroup vg1 = new MyCardView(getBaseContext());
    ViewGroup.MarginLayoutParams layoutParams1 = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,200);
    layoutParams1.setMargins(0,10,0,0);
    vg1.setBackgroundColor(Color.WHITE);
    ViewCompat.setElevation(vg1, 5);

    TextView tvTemp1 = new TextView(getBaseContext());
    tvTemp1.setTextColor(Color.BLACK);
    tvTemp1.setText("Hello There 2");

    vg1.addView(tvTemp1);

    linearLayout.addView(vg,layoutParams);
    linearLayout.addView(vg1,layoutParams1);

    frame.addView(linearLayout);
}

现在发生的事情是我试图设置 ViewGroup 的边距,但这似乎并不适用。请帮忙。

最佳答案

我犯的错误是,在我的代码中,我声明了一个 ViewGroup.MarginLayoutParams layoutParams1 的实例,并且我在 linearLayout.addView(vg1,layoutParams1)< 中传递了相同的实例 但方法 addView() 接受 View 作为第一个参数,并接受 LayoutParams 实例作为第二个参数。

所以我认为这就是问题所在(不完全确定)。所以我将代码更改为

LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,200);

关于java - setMargins 方法不适用于自定义 ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33317439/

相关文章:

javascript - Worklight - Paho MQTT JavaScript : Always timeout on Android device and works in simulator

android - 如何从资源中设置位图

android - Android Toolbar添加自定义 View 时,会有一个marginLeft

java - 可以使用 iText 将 pdf 连接/合并在一起的函数 - 导致一些问题

java - Eclipse 中 ECLemma 代码覆盖工具的奇怪行为

android - 如何在 Android Oreo 上注册 ACTION_PACKAGE_ADDED 和 ACTION_PACKAGE_REMOVED?

android - 如何为自定义 View 类设置可绘制属性

Java Web 服务允许大文件流

java - 安卓填充局部圆弧

android - 在 Android 中使用大尺寸可绘制对象和自定义 View