android - 如何修复此布局错误。按钮未正确放置?

标签 android android-layout

我遇到布局问题。

在 Samsung j+ 中打开应用程序时,按钮位置不正确。请帮忙。 图片中解释了问题,请查看图片。

this is the image...

这是xml代码:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    android:gravity="top"
    android:id="@+id/relativeLayout">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:gravity="right" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/relativeLayout"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="75sp"
        android:layout_height="75sp"
        android:text="."
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:textSize="25dp"
        android:layout_toRightOf="@+id/button3"
        android:layout_toLeftOf="@+id/button12"
        android:layout_toStartOf="@+id/button12" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/button2"
        android:layout_above="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/button5"
        android:textSize="25dp" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/button3"
        android:layout_toEndOf="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:layout_alignTop="@+id/button5"
        android:layout_above="@+id/button4"
        android:textSize="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:id="@+id/button4"
        android:layout_below="@+id/button2"
        android:layout_alignRight="@+id/button3"
        android:layout_alignEnd="@+id/button3"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="25dp" />

    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text="3"
        android:id="@+id/button5"
        android:layout_above="@+id/button"
        android:layout_toRightOf="@+id/button4"
        android:layout_toEndOf="@+id/button4"
        android:textSize="25dp" />

    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text="4"
        android:id="@+id/button6"
        android:layout_above="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="25dp" />

    <Button
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:text="5"
        android:id="@+id/button7"
        android:layout_alignTop="@+id/button6"
        android:layout_toRightOf="@+id/button6"
        android:layout_toEndOf="@+id/button6"
        android:layout_alignBottom="@+id/button6"
        android:textSize="25dp" />

    <Button
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:text="6"
        android:id="@+id/button8"
        android:layout_above="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:layout_toEndOf="@+id/button3"
        android:layout_alignTop="@+id/button7"
        android:textSize="25dp" />

    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text="7"
        android:id="@+id/button9"
        android:layout_above="@+id/button8"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        android:id="@+id/button10"
        android:layout_alignTop="@+id/button9"
        android:layout_toRightOf="@+id/button9"
        android:layout_above="@+id/button6"
        android:layout_alignRight="@+id/button7"
        android:layout_alignEnd="@+id/button7"
        android:textSize="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"
        android:id="@+id/button11"
        android:layout_above="@+id/button8"
        android:layout_toRightOf="@+id/button10"
        android:layout_alignTop="@+id/button10"
        android:layout_alignRight="@+id/button8"
        android:layout_alignEnd="@+id/button8"
        android:textSize="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="="
        android:id="@+id/button12"
        android:layout_toRightOf="@+id/button5"
        android:layout_below="@+id/button8"
        android:layout_alignParentBottom="true"
        android:textSize="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:id="@+id/button13"
        android:layout_alignTop="@+id/button11"
        android:layout_toRightOf="@+id/button11"
        android:layout_alignRight="@+id/button12"
        android:layout_alignEnd="@+id/button12"
        android:layout_alignBottom="@+id/button8"
        android:textSize="25dp" />

    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:text="/"
        android:id="@+id/button14"
        android:layout_above="@+id/button9"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/button10"
        android:layout_alignEnd="@+id/button10"
        android:textSize="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:id="@+id/button15"
        android:layout_alignBottom="@+id/button14"
        android:layout_alignTop="@+id/button14"
        android:layout_alignRight="@+id/button13"
        android:layout_alignEnd="@+id/button13"
        android:layout_toRightOf="@+id/button11"
        android:layout_toEndOf="@+id/button11"
        android:textSize="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="*"
        android:id="@+id/button16"
        android:layout_alignBottom="@+id/button14"
        android:layout_alignTop="@+id/button14"
        android:layout_toRightOf="@+id/button14"
        android:layout_toLeftOf="@+id/button15"
        android:layout_toStartOf="@+id/button15"
        android:textSize="25dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLEAR SCREEN"
        android:id="@+id/button17"
        android:layout_above="@+id/button14"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/button15"
        android:layout_alignEnd="@+id/button15" />

</RelativeLayout>

最佳答案

无论你使用什么设备,这里都有一个完整的响应式 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:layout_weight="2"
    android:text="CLEAR SCREEN" />

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent" 
        android:layout_weight="2"
        android:text="/"
        android:textSize="25dp" />

    <Button
        android:id="@+id/button15"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="-"
        android:textSize="25dp" />

    <Button
        android:id="@+id/button16"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="*"
        android:textSize="25dp" />

    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

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

            <Button
                android:id="@+id/button9"
                android:layout_width="0dp"
                android:layout_height="match_parent" 
                android:layout_weight="1"
                android:text="7"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button10"
                android:layout_width="0dp"
                android:layout_height="match_parent" 
                android:layout_weight="1"
                android:text="8"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button11"
                android:layout_width="0dp"
                android:layout_height="match_parent" 
                android:layout_weight="1"
                android:text="9"
                android:textSize="25dp" />
        </LinearLayout>

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

            <Button
                android:id="@+id/button6"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="4"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button7"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="5"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button8"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="6"
                android:textSize="25dp" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="+"
            android:id="@+id/button13" 
            android:textSize="25dp" />
    </LinearLayout>
    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

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

            <Button
                android:id="@+id/button2"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="1"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button3"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="match_parent" 
                android:layout_weight="1"
                android:text="2"
                android:textSize="25dp" />

            <Button
                android:id="@+id/button5"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="3"
                android:textSize="25dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:text="0"
                android:id="@+id/button4" 
                android:textSize="25dp" />
            <Button
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="."
                android:id="@+id/button" 
                android:textSize="25dp" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="="
            android:id="@+id/button12"
            android:layout_toRightOf="@+id/button5"
            android:layout_below="@+id/button8"
            android:layout_alignParentBottom="true"
            android:textSize="25dp" />
    </LinearLayout>
    </LinearLayout>

</LinearLayout>

关于android - 如何修复此布局错误。按钮未正确放置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35205481/

相关文章:

安卓 : EditText Soft Keyboard is not hiding at first time press

android - 我们应该在 Strings.xml 中还是在布局 xml 文件本身中给出字符串值

java - 将 fragment 布局纵向更改为横向

android - Android 中的 View.OnClickListener() 响应错误的按钮

android - 为什么在 Android 上,OpenCV 摄像头在捕获视频时比 Android 摄像头更快

android - 我的相机 View 与 GLSurfaceView 重叠...但我希望它被 GLSurfaceView 覆盖

android - 如何设置布局 :width of an object to (wrap_content + 10)?

java - 缺少类日期选择器

android - 如何更改 Android DatePicker 中的按钮

Android Studio - 无法从 SVN 分支合并