android - RelativeLayout 中的按钮对齐不正确

标签 android android-layout android-relativelayout

我在一个 LinearLayout 中使用了多个 RelativeLayouts。用于按钮的 RelativeLayout 导致外观不正确,如下所示:

enter image description here

这是我的.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wood"
    android:orientation="vertical"
    android:padding="25dp" >

<RelativeLayout
       android:id="@+id/relative"
       android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/button1"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/button2"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

 <RelativeLayout
       android:id="@+id/relative2"
       android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/btn5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <Button
        android:id="@+id/btn8"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <Button
        android:id="@+id/btn7"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btn8"
        android:text="Button" />

    <Button
        android:id="@+id/btn6"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btn7"
        android:text="Button" />

</RelativeLayout>

</LinearLayout>

如何在不使用 Grid View 的情况下正确且漂亮地对齐我的按钮,因为它需要额外的库和 API 14 及更高版本?谢谢。

最佳答案

代替 RelativeLayouts 使用水平方向的 LinearLayouts:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wood"
    android:orientation="vertical"
    android:padding="25dp" >

<LinearLayout
   android:id="@+id/relative"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/button4"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

<LinearLayout
   android:id="@+id/relative2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">

<Button
    android:id="@+id/btn5"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/btn8"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/btn7"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/btn6"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>
</LinearLayout>

关于android - RelativeLayout 中的按钮对齐不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750119/

相关文章:

java - logcat 中应用程序强制关闭并出现 NullPointerException

android - 在android中通过蓝牙进行多连接

java - 如何在Android中逐行添加连续的布局

android - 如何在android中放置多个 TextView 和 ImageView ?

android - 使用RelativeLayout构建像ui这样的网格时出现问题

java - v4 androidmanifest.xml 文件未找到

android - 与 RecyclerView 的数据绑定(bind)未正确呈现

Android 前台服务 - 不显示三星 Galaxy 设备灯的通知

android - 自定义通知最大高度?

android - 当布局宽度为 720 时,RelativeLayout.getWidth 返回 0