简体   繁体   中英

Layout changes when android:id is changed

When I change the android:id in xml file items in layout becomes unorganised 之前

when android:id is changed in xml file layout becomes like this 在此处输入图片说明

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rms.Suppliers" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="Supplier DETAIL" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="18dp"
    android:layout_marginTop="40dp"
    android:layout_toLeftOf="@+id/textView1"
    android:text="NAME" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/editText3"
    android:ems="20"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView5"
    android:layout_alignBottom="@+id/textView5"
    android:layout_toRightOf="@+id/button1"
    android:ems="10"
    android:inputType="phone" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView4"
    android:layout_alignBottom="@+id/textView4"
    android:layout_toRightOf="@+id/button1"
    android:ems="20"
    android:inputType="textPostalAddress" />

<Button
    android:id="@+id/button3"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_alignBaseline="@+id/button4"
    android:layout_alignBottom="@+id/button4"
    android:layout_alignLeft="@+id/button2"
    android:text="Delete" />

<Button
    android:id="@+id/button2"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_above="@+id/button4"
    android:layout_alignRight="@+id/textView1"
    android:text="Modify" />

<Button
    android:id="@+id/button6"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_alignBaseline="@+id/button5"
    android:layout_alignBottom="@+id/button5"
    android:layout_alignLeft="@+id/button3"
    android:text="Show" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView2"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="22dp"
    android:text="ADDRESS" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="15dp"
    android:text="CONTACT" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView5"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="17dp"
    android:text="Item" />

<Button
    android:id="@+id/button4"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_below="@+id/button1"
    android:layout_toLeftOf="@+id/button3"
    android:text="View" />

<Button
    android:id="@+id/button5"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_below="@+id/button3"
    android:layout_toLeftOf="@+id/editText2"
    android:text="View all" />

<Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_alignLeft="@+id/button4"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="96dp"
    android:text="Add" />

i got the reason why this happening because i am using RelativeLayout then using toLeftOf, belowOf but what is the solution i mean is there any code/key word to make it permanemt while i want to use realtive layout only any help would be appreciated and please don't mind if its childish I am beginner

Your button1 has a layout_below attribute that uses id of textView3

If you change id of textView3 to textView33 , you also have to make change in button1 like this as the id of textView3 is referred in button1

<Button
  android:id="@+id/button1"
  android:layout_width="100dp"
  android:layout_height="40dp"
  android:layout_alignLeft="@+id/button4"
  android:layout_below="@+id/textView33"  //had to make one change here too
  android:layout_marginTop="96dp"
  android:text="Add" />

As you are using relative layout, views used in your layout like TextView, Button, EditText etc... are placed relative to each other. For this relative placing of views in layout, it will use the id (android:id="@+id) as reference.

for ex:

<Button
        android:id="@+id/button5"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_below="@+id/button3"
        android:layout_toLeftOf="@+id/editText2"
        android:text="View all" />

Button is placed to left of editText2 .

So don't change the ids once created. If you want to change, again you may need to re arrange the views (like textview, button ....)

In references, don't write '@+id', make the reference with only '@id'.

For example, change:

android:layout_toLeftOf="@+id/editText2"

to:

android:layout_toLeftOf="@id/editText2"

You should only have in mind that your reference must be declared before hand (in this case, editText2 must be declared above button5 in your xml file)

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