I want to draw a TV remote interface on an Android Activity screen. But I don't succeed in getting the buttons to the right places. Because I'm using a grid internally to init the buttons (programmatically by the way) I thought using a GridLayout and placing the buttons as subviews should be the best way. (I was trying it with TableLayout before, but I couldn't find out how to init a button that has a height going over two or more TableRows.)
我想在Android活动屏幕上绘制电视遥控器界面。但是我没有成功地将按钮送到正确的位置。因为我在内部使用网格来初始化按钮(按编程方式)我认为使用GridLayout并将按钮作为子视图放置应该是最好的方法。 (我之前尝试使用TableLayout,但是我无法找到如何初始化一个高度超过两个或更多TableRows的按钮。)
But the GridLayout class automatically resets all buttons when I change the size of one button, as a result my view looks pretty bad. Is there a way to set the exact places where my buttons should be using a GridLayout? Or would you recommend another type of Layout?
但是当我更改一个按钮的大小时,GridLayout类会自动重置所有按钮,因此我的视图看起来非常糟糕。有没有办法使用GridLayout设置我的按钮应该使用的确切位置?或者你会推荐其他类型的布局?
This is what I expect the whole thing to look like at the end (except the "Endgeräte auswählen" button):
这就是我期望在最后看到的整个事情(除了“Endgeräteususwählen”按钮):
1 个解决方案
#1
1
try relative layout :
尝试相对布局:
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);
#1
1
try relative layout :
尝试相对布局:
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);