Android for Remote Control界面中哪种网格状布局?

时间:2013-01-22 13:29:42

标签: android layout button size grid-layout

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

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

这就是我期望在最后看到的整个事情(除了“Endgeräteususwählen”按钮):

enter image description here

1 个答案:

答案 0 :(得分: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);