简体   繁体   中英

Custom Alert button overlapping

Ok i successfully created a custom alert and when i ran it the format is the same as in xml but after re-running it for the nth time suddenly the buttons overlap and I dont know how to fix it so can you pls help me out on this?.

alert.xml(output should be):
在此处输入图片说明

wrong output:
在此处输入图片说明

xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#595959">
    <EditText
        android:id="@+id/orderQuantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/b3"
        android:background="#FFFFFF"
        android:ems="10"
        android:inputType="number" />

     <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/b4"
        android:layout_alignParentLeft="true"
        android:text="1" />

     <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/orderQuantity"
        android:layout_toRightOf="@+id/b4"
        android:text="2" />
       <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/b4"
        android:layout_centerHorizontal="true"
        android:text="3" />
     <Button
        android:id="@+id/b4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/b2"
        android:text="4" />

    <Button
        android:id="@+id/b5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b2"
        android:layout_toLeftOf="@+id/b3"
        android:text="5" />

    <Button
        android:id="@+id/b6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/b5"
        android:layout_alignBottom="@+id/b5"
        android:layout_alignLeft="@+id/b3"
        android:text="6" />

    <Button
        android:id="@+id/b7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b4"
        android:layout_toLeftOf="@+id/b5"
        android:text="7" />

    <Button
        android:id="@+id/b8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b4"
        android:layout_toRightOf="@+id/b4"
        android:text="8" />

    <Button
        android:id="@+id/b9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/b6"
        android:layout_below="@+id/b6"
        android:text="9" />

    <Button
        android:id="@+id/b0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/b8"
        android:layout_toLeftOf="@+id/b9"
        android:text="0" />

    <Button
        android:id="@+id/addCart"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/b0"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/b7"
        android:layout_toLeftOf="@+id/b0"
        android:text="ADD" />

    <Button
        android:id="@+id/cancel"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/b0"
        android:layout_alignRight="@+id/b9"
        android:layout_below="@+id/b9"
        android:layout_toRightOf="@+id/b0"
        android:text="BACK" />


</RelativeLayout>

Use LinearLayout instead of RelativeLayout. And remove these in Buttons:

android:layout_alignParentLeft="true"

after that it should be fine.

EDIT: Not just left alignment directives, but all alignment directives.

Organize each row as below...

 <Button
     android:id="@+id/b1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_above="@+id/b4"
     android:layout_alignParentLeft="true"
     android:text="1" />

 <Button
     android:id="@+id/b2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/orderQuantity"
     android:layout_toLeftOf="@+id/b3"
     android:layout_toRightOf="@+id/b1"
     android:text="2" />

 <Button
     android:id="@+id/b3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_above="@+id/b4"
     android:layout_toRightOf="@+id/b2"
     android:layout_centerHorizontal="true"
     android:text="3" />

i would suggest you to use TableLayout ... very easy to understand and fits what you want perfectly and its easy to reuse or modify later ... your code ...

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <EditText
            android:id="@+id/orderQuantity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignRight="@+id/b3"
            android:background="#FFFFFF"
            android:ems="10"
            android:inputType="number" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="2" />

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="3" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="4" />

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="5" />

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="6" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" 
            android:text="7"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" 
            android:text="8"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="9" />
    </TableRow>


    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" 
            android:text="Add"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" 
            android:text="0"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" 
            android:text="OK"/>
    </TableRow>

</TableLayout>

Ok after analyzing i was able to see whats wrong.
This

<Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/b4"
        android:layout_centerHorizontal="true"
        android:text="3" />

Should be :

 <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/b4"
        android:text="3" />

I would do something like this:

<?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="wrap_content"
              android:orientation="vertical"
              android:background="#595959">
    <EditText
            android:id="@+id/orderQuantity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignRight="@+id/b3"
            android:background="#FFFFFF"
            android:ems="10"
            android:inputType="number"/>

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

        <Button
                android:id="@+id/b1"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="1"/>

        <Button
                android:id="@+id/b2"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="2"/>
        <Button
                android:id="@+id/b3"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="3"/>

    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:weightSum="1">

        <Button
                android:id="@+id/b4"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="4"/>

        <Button
                android:id="@+id/b5"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="5"/>
        <Button
                android:id="@+id/b6"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="6"/>

    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:weightSum="1">

        <Button
                android:id="@+id/b7"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="7"/>

        <Button
                android:id="@+id/b8"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="8"/>
        <Button
                android:id="@+id/b9"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="9"/>

    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:weightSum="1">
        <Button
                android:id="@+id/b0"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="0"/>

        <Button
                android:id="@+id/addCart"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="ADD"/>

        <Button
                android:id="@+id/cancel"
                android:layout_width="0dp"
                android:layout_weight=".3333"
                android:layout_height="match_parent"
                android:text="BACK"/>
    </LinearLayout>

</LinearLayout>

Dial_portrait

Dial_landscape

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM