简体   繁体   中英

How Can I independent the views in a layout

How can I move views independently from other views in a layout ? For example, I want to be able to move one button up without the other moving but, currently, they are both moving. For more information please see the following image:

在此处输入图片说明

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button"
    android:layout_toEndOf="@+id/button"
    />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button3"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="39dp" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button4"
    android:layout_alignTop="@+id/button3"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignStart="@+id/button2" />

First you not posted complete xml layout file. How can some some know what layout you are using.

Solution:You need to put both buttons in different layout if your are using LinearLayout or you can use Relative Layout but in Relative Layout case both buttons should not be depend on each other.

You can use a RelativeLayout to embed the components you want.

 <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:orientation="vertical"
                    android:padding="10dp">

                    <TextView
                        android:id="@+id/cardholderNameText"
                        style="@style/form_label"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="10dp"
                        android:text="@string/cardholder_name" />
  </RelativeLayout>

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