如何以编程方式排列按钮?

时间:2015-01-15 20:23:19

标签: java android

我一直试图让标准D-Pad方向的控制箭头居中并位于视频流的下方,但这是我能够做到的最好的基于我认为应该做的事情。是正确的代码。请注意,向上和向下箭头位于正确的位置,但左箭头不显示,右箭头位于左侧。要么我没有正确理解某些东西,要么有影响箭头排列的东西,我没有捕捉到。

   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);

这就是它的样子: http://s23.postimg.org/otbyon2a3/screenie.png

1 个答案:

答案 0 :(得分:0)

网格布局怎么样?

<?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,您应该使用类似的布局。