Android中的布局包括
:线性布局、表格布局、相对布局、帧布局和绝对布局;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led2off" android:id="@+id/btnLED2Off"
android:layout_weight="1" android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led1off" android:id="@+id/btnLED1Off"
android:layout_weight="0.1" android:layout_toLeftOf="@id/btnLED2Off"
></Button>
<Button android:layout_height="wrap_content" android:text="@string/led1on" android:layout_width="wrap_content" android:id="@+id/btnLED1On"
android:layout_weight="1" android:layout_toLeftOf="@id/btnLED1Off"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led2on" android:id="@+id/btnLED2On"
android:layout_weight="1" android:layout_toLeftOf="@id/btnLED1On"
android:layout_alignTop="@id/btnLED2Off"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led3off" android:id="@+id/btnLED3Off"
android:layout_weight="1" android:layout_below="@id/btnLED2Off"
></Button>
<Button android:layout_height="wrap_content" android:text="@string/led3on" android:layout_width="wrap_content" android:id="@+id/btnLED3On"
android:layout_weight="1" android:layout_below="@id/btnLED3Off"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led4on" android:id="@+id/btnLED4On"
android:layout_weight="1" android:layout_below="@id/btnLED3On"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led4off" android:id="@+id/btnLED4Off"
android:layout_weight="1"
android:layout_below="@id/btnLED4On"
></Button>
<!-- 我的注释-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentLeft="true"-->
<!--android:layout_marginTop="295dp"-->
</RelativeLayout>
上面的代码就是界面布局的XML语句;
主要是调整位置;
</RelativeLayout>这个表示 相对布局;
android:layout_width="wrap_content" //按钮大小适应字体大小
android:layout_height="wrap_content"//按钮大小适应字体大小
android:layout_alignParentRight="true" //靠在父窗口的最右边
android:layout_toLeftOf="@id/btnLED2Off"//靠在按钮LED2OFF的左边
android:layout_below="@id/btnLED2Off"//靠在按钮LED2OFF下边
控件类:
看来写博客确实耐心,我还是记录些代码吧:
最后调整后的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led2off" android:id="@+id/btnLED2Off"
android:layout_weight="1" android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led1off" android:id="@+id/btnLED1Off"
android:layout_weight="0.1" android:layout_toLeftOf="@id/btnLED2Off"
android:layout_marginLeft="10px"
></Button>
<Button android:layout_height="wrap_content" android:text="@string/led1on" android:layout_width="wrap_content" android:id="@+id/btnLED1On"
android:layout_weight="1" android:layout_toLeftOf="@id/btnLED1Off"
android:layout_marginLeft="10px"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led2on" android:id="@+id/btnLED2On"
android:layout_weight="1" android:layout_toLeftOf="@id/btnLED1On"
android:layout_marginLeft="10px"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led3off" android:id="@+id/btnLED3Off"
android:layout_weight="1" android:layout_below="@id/btnLED2Off"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
></Button>
<Button android:layout_height="wrap_content" android:text="@string/led3on" android:layout_width="wrap_content" android:id="@+id/btnLED3On"
android:layout_weight="1"
android:layout_toLeftOf="@id/btnLED3Off"
android:layout_below="@id/btnLED2Off"
android:layout_marginLeft="10px"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led4on" android:id="@+id/btnLED4On"
android:layout_weight="1" android:layout_toLeftOf="@id/btnLED3On"
android:layout_below="@id/btnLED1Off"
android:layout_marginLeft="10px"
></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/led4off" android:id="@+id/btnLED4Off"
android:layout_weight="1"
android:layout_below="@id/btnLED2On"
android:layout_toLeftOf="@id/btnLED4On"
></Button>
<!-- 我的注释-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentLeft="true"-->
<!--android:layout_marginTop="295dp"-->
</RelativeLayout>