Java 如何以编程方式排列按钮?
我一直在尝试将控制箭头置于标准D-Pad方向的中心,并置于视频流的下方,但这是迄今为止我所能做的最好的,基于我认为应该是正确的代码。请注意,上下箭头位于正确位置,但左箭头不显示,右箭头位于左侧。要么我没有正确地理解某些东西,要么有一些东西影响了我没有捕捉到的箭头的排列Java 如何以编程方式排列按钮?,java,android,Java,Android,我一直在尝试将控制箭头置于标准D-Pad方向的中心,并置于视频流的下方,但这是迄今为止我所能做的最好的,基于我认为应该是正确的代码。请注意,上下箭头位于正确位置,但左箭头不显示,右箭头位于左侧。要么我没有正确地理解某些东西,要么有一些东西影响了我没有捕捉到的箭头的排列 RelativeLayout rl = new RelativeLayout(this); RelativeLayout.LayoutParams relativeLayoutParams =
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
setContentView(rl, relativeLayoutParams);
mv = new MjpegView(this);
rl.addView(mv);
Button panLeft = new Button(this);
panLeft.setBackgroundResource(R.drawable.arrow_left);
int panLeftID = View.generateViewId();
panLeft.setId(panLeftID);
Button panRight = new Button(this);
panRight.setBackgroundResource(R.drawable.arrow_right);
int panRightID = View.generateViewId();
panRight.setId(panRightID);
Button tiltUp = new Button(this);
tiltUp.setBackgroundResource(R.drawable.arrow_up);
int tiltUpID = View.generateViewId();
tiltUp.setId(tiltUpID);
Button tiltDown = new Button(this);
tiltDown.setBackgroundResource(R.drawable.arrow_down);
int tiltDownID = View.generateViewId();
tiltDown.setId(tiltDownID);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(tiltDown, relativeLayoutParams);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.LEFT_OF, tiltUpID);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(panLeft, relativeLayoutParams);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, tiltDownID);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(panRight, relativeLayoutParams);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ABOVE, tiltDownID);
relativeLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(tiltUp, relativeLayoutParams);
下面是它的样子:
网格布局怎么样
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="3"
android:orientation="horizontal"
android:id="@+id/dpad_grid"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up"
android:id="@+id/button4"
android:layout_row="0"
android:layout_column="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:id="@+id/button3"
android:layout_row="1"
android:layout_column="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down"
android:id="@+id/button2"
android:layout_row="2"
android:layout_column="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
android:id="@+id/button5"
android:layout_row="1"
android:layout_column="0"/>
</GridLayout>
要回显@Dave S,您真的应该为类似的内容使用布局。您为视图设置的ID是什么,它们是否都是不同的且>0?我只是对每个视图使用如下方法:int-panRightID=view.generateView;panRight.setIdpanRightID;我假设这些方法正在创建有效的ID。旁注:这些注释中是否有格式化的教程?我可以告诉您,当您尝试添加panLeft tiltUp时,不会将其添加到视图中,因此无法将其放置在任何内容的左侧。我也会使用XML进行所有操作,你的方法感觉很尴尬,而且更复杂,需要在IMO中使用。我真的希望我可以,阅读我上面关于为什么我现在不能使用的解释。你可能可以通过编程方式生成这个,而不需要太多麻烦。我们的网站:你可以发布你的视频流代码,我们可以看一下。我们可以通过电子邮件看一下吗?我不能在这里发布代码,因为它太长了。当我使用这里的layoutInflater示例时,这个布局对我很有效:谢谢!