android - 不同宽度的按钮未与 layout_weight 正确对齐

标签 android android-layout

我正在制作一个 fragment 中的自定义键盘。按键位于不同的行中,大多数宽度相同,但在一行中有一个键是双倍宽度的。但是,由于边距设置,键不会垂直对齐。我将在下面包含一个简化的图像。

enter image description here

“按钮”实际上是 RelativeLayout,它将容纳可绘制对象,使它们看起来像按钮。我尝试将 layout_margin 更改为 padding,这确实纠正了垂直对齐问题,但它消除了键之间的间距。我想我可以将每个键的 RelativeLayout 放在 FrameLayout 中,但是我在 fragment 中已经有太多的 View 和布局,我担心加载时间慢。

澄清更新:我实际上希望最后一个按钮是双倍宽度,但只是与上一行中倒数第二个按钮的左侧对齐。

这是布局的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:layout_weight="2"
        android:background="#ff494949" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
</LinearLayout>

</LinearLayout>

最佳答案

由于您缺少一个单元格,因此边距与权重混淆。为了弥补 2dp,您需要进行大量计算,因为并非所有设备都具有相同的 dp。这就是你要做的。创建一个名为 rect_bg.xml 的 xml 文件或任何你想命名的文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
     <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
        </shape>
    </item>


    <item
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#ff494949"/>
        </shape>
    </item>

</layer-list>

并用填充替换所有边距:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="1"
            android:background="@drawable/rect_bg" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="2dp"
            android:layout_weight="2"
            android:background="@drawable/rect_bg" >

            <TextView
                android:background="#ffffff"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

它适用于所有设备(检查预览)以及横向和纵向。

关于android - 不同宽度的按钮未与 layout_weight 正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25861699/

相关文章:

Android使用按钮和对话框复选框进行多项选择,并在 ListView 中显示所选项目而不是按钮

android - 将gradle从3.4升级到3.5会在生成的DataBindingClasses中抛出 “error: cannot find symbol class”和 “error: package … does not exist”

android - 如何在android中隐藏用户界面

android - 单击 EditText 打开 DatePickerDialog 需要两次单击

java - Android Sqlite 表未创建

java - 获取手机信号塔位置 - Android

ADT : type cannot be resolved error 中的 Android 库

安卓谷歌地图圆形

android-layout - 在 Robotium 中,单击 QuickAction 图标

android - 选项卡布局从底部被切割