0

layout_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".MainActivity" >

    <com.example.testandroid.MainView 
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

主视图:

public class MainView extends ViewGroup {
    public MainView(Context context, AttributeSet as) {
        super(context);
        addView(new ZoomController(context));
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0, len = getChildCount(); i < len; i++) {
            View v = getChildAt(i);
            v.layout(0, 0, r - l, b - t);
        }
    }
}

缩放控制器:

public class ZoomController extends LinearLayout {
    private Button zoomIn;
    public ZoomController(Context context) {
        super(context);
        zoomIn = new Button(context);
        zoomIn.setText("+");

        setOrientation(1);
        addView(zoomIn);
    }
}

当我运行应用程序时,我得到一个空白屏幕。

为什么zoomIn按钮不可见?

4

2 回答 2

0

这可能不是您正在寻找的答案,但如果您有 MainView 扩展 LinearLayout 而不是 ViewGroup 它可以正常工作。

于 2013-05-07T04:49:04.013 回答
0

请试一试。。

公共类 MainActivity 扩展 Activity

{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.buttonLayout);
    linearLayout.addView(new ZoomController(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public class ZoomController extends LinearLayout {
    private Button zoomIn;

    public ZoomController(Context context) {
        super(context);
        zoomIn = new Button(context);
        zoomIn.setText("+");

        setOrientation(1);
        addView(zoomIn);
    }
}

}

于 2013-05-07T08:03:21.633 回答