0

我想在 Android Activity 屏幕上绘制一个电视遥控器界面。但是我没有成功地将按钮放在正确的位置。因为我在内部使用网格来初始化按钮(顺便以编程方式),所以我认为使用 GridLayout 并将按钮放置为子视图应该是最好的方法。(我之前尝试过使用 TableLayout,但我不知道如何初始化一个高度超过两个或更多 TableRows 的按钮。)

但是当我更改一个按钮的大小时,GridLayout 类会自动重置所有按钮,因此我的视图看起来很糟糕。有没有办法设置我的按钮应该使用 GridLayout 的确切位置?或者你会推荐另一种布局吗?

这就是我期望整个事情最后的样子(“Endgeräteauswählen”按钮除外):

在此处输入图像描述

4

1 回答 1

1

尝试相对布局:

RelativeLayout Main = new RelativeLayout(this);
    RelativeLayout.LayoutParams viewParamsCenter = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
    Main.setLayoutParams(viewParamsCenter);

    Button but = new Button(this);
    but.setText("BTN");
    but.setBackgroundResource(R.drawable.ic_launcher);
    viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    but.setLayoutParams(viewParamsCenter);
    Main.addView(but);

    but = new Button(this);
    but.setText("BTN 2");
    but.setBackgroundResource(R.drawable.ic_launcher);
    viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    but.setLayoutParams(viewParamsCenter);
    Main.addView(but);

    but = new Button(this);
    but.setText("Center");
    but.setId(998900);
    but.setBackgroundResource(R.drawable.ic_launcher);
    viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
    but.setLayoutParams(viewParamsCenter);
    Main.addView(but);

    but = new Button(this);
    but.setText("below");
    but.setBackgroundResource(R.drawable.ic_launcher);
    viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    viewParamsCenter.addRule(RelativeLayout.BELOW, 998900);
    viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
    but.setLayoutParams(viewParamsCenter);
    Main.addView(but);

    but = new Button(this);
    but.setText("Center above");
    but.setBackgroundResource(R.drawable.ic_launcher);
    viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    viewParamsCenter.addRule(RelativeLayout.ABOVE, 998900);
    viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
    but.setLayoutParams(viewParamsCenter);
    Main.addView(but);

    but = new Button(this);
    but.setText("Center left");
    but.setBackgroundResource(R.drawable.ic_launcher);
    viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    viewParamsCenter.addRule(RelativeLayout.LEFT_OF, 998900);
    viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
    but.setLayoutParams(viewParamsCenter);
    Main.addView(but);

    but = new Button(this);
    but.setText("Center right");
    but.setBackgroundResource(R.drawable.ic_launcher);
    viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    viewParamsCenter.addRule(RelativeLayout.RIGHT_OF, 998900);
    viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
    but.setLayoutParams(viewParamsCenter);
    Main.addView(but);

    but = new Button(this);
    but.setText("Bottom");
    but.setBackgroundResource(R.drawable.ic_launcher);
    viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    but.setLayoutParams(viewParamsCenter);
    Main.addView(but);

    RelativeLayout Main_SUPER = (RelativeLayout) findViewById(R.id.relativeLayout);
    Main_SUPER.addView(Main);
于 2013-01-22T13:54:58.637 回答