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

試してみてください..

public class 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 に答える