【问题标题】:When Extending LinearLayout Child Views are not visible当扩展 LinearLayout 子视图不可见时
【发布时间】:2011-06-09 16:31:48
【问题描述】:

好的。所以我在 Main 中开始我的活动,获取 FragmentManager 并实例化一个需要返回 View 的 Fragment。好的。所以我扩展了一个 LinearLayout 以便返回一些东西。我的 Activity 和 Fragment 很高兴,但我不高兴。

我在父 ViewGroup 中创建的三个 LinearLayout 在那里(代码如下)。我已经通过计算孩子数量和设置背景颜色相互对比来验证这一点。父级也会根据我让子级变得多高来改变大小(当我没有在父级上声明任何 LayoutParams 时)。

public class Mainmenu extends LinearLayout {
private ArrayList<LinearLayout> panes = new ArrayList<LinearLayout>();
private Context context;
private final int
    LEFT = 0, CENTER = 1, RIGHT = 2;

public Mainmenu(Context c) {
    super(c);
    context = c;
    setBackgroundColor(Color.WHITE);
    setOrientation(HORIZONTAL);
    setLayoutParams(
            new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT));


    for(int i=0;i<=RIGHT;i++){                                      //Create the (3) Panes
        LinearLayout ll = new LinearLayout(context);
        ll.setLayoutParams(
                new LinearLayout.LayoutParams(300,
                        LinearLayout.LayoutParams.FILL_PARENT));
        switch(i){
        case LEFT | RIGHT:
            ll.setBackgroundColor(Color.DKGRAY);
        default:
            ll.setBackgroundColor(Color.BLACK);
        }
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setVisibility(VISIBLE);
        ll.setWillNotDraw(false);
        panes.add(i, ll);
        addView(ll);
    }

    LinearLayout.LayoutParams buttons =
        new LinearLayout.LayoutParams(100, 50);
    buttons.setMargins(15, 5, 5, 0);

    TextView tv1 = new TextView(context);
    tv1.setText("hello");
    tv1.setTextColor(Color.RED);
    panes.get(LEFT).addView(tv1, buttons);

    Button button = new Button(context);
    button.setText("Launch Editor");
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v){
        }
    });
    panes.get(CENTER).addView(button);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // TODO Auto-generated method stub

}

}

没有什么是空的,我的所有元素(3 个视图组和 2 个视图)都存在于树中但不可见。我尝试通过父母和孩子将孩子带到最前面,以不同的 super.methods 创建它们,并以类似的散弹方式使视图无效。这是怎么回事?不知道自己在做什么就这么简单吗?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    问题仅仅是因为您覆盖了 onLayout 并且什么都不做。如果您想自己布局孩子(即,您正在设计一些独特的自定义布局),您只需要覆盖它。在这种情况下,只需删除该方法,或调用 super.onLayout。

    【讨论】:

    • 哇...在某些时候我正在扩展一些需要被覆盖的东西。非常感谢。
    猜你喜欢
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 2017-03-10
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多