简体   繁体   中英

How to fix the button position in Linear Layout

I define two components (One text View and one Button) in Linear layout.

<LinearLayout 
android:id="@+id/layout_1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/edit_view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize = "15sp"
    android:layout_weight="5"
    android:layout_marginTop="20dp"
    android:layout_alignParentLeft="true"
    android:gravity="center"
    android:hint="Write Greeting" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_toRightOf="@+id/edit_view1"
    android:layout_alignParentRight="true"
    android:background="@android:color/transparent"
    android:src="@drawable/e301"/>
</LinearLayout>

If I write long text, the position of my ImageButton is changed. I want to fix the position. Any suggestion?

Replace your layout contents as :

<LinearLayout 
android:id="@+id/layout_1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/edit_view1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textSize = "15sp"
    android:layout_weight="5"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:hint="Write Greeting" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/transparent"
    android:src="@drawable/e301"/>
</LinearLayout>

You can't use RelativeLayout's properties of alignment inside Linear layout.

See your modified code:

<LinearLayout 
android:id="@+id/layout_1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:singleLine="false"
    android:id="@+id/edit_view1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textSize = "15sp"
    android:layout_marginTop="20dp"
    android:text="etegtdrgrgbvcvbghgfffffffffffffffffffffffffffff"
    android:gravity="center"
    android:hint="Write Greeting" 
    android:layout_weight="2"/>

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:layout_weight="1"

    android:background="@android:color/transparent"
    android:src="@drawable/ic_launcher"
    />
</LinearLayout>

Keep layout width as 0 dp using weight and weightSum, so it will adjust. Also, keep sengleLine as false for textview.

Output:

在此处输入图片说明

Hope this helps.

Try below code:-

<LinearLayout
    android:id="@+id/layout_1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/edit_view1"
        android:layout_width="0dp"
        android:layout_weight="5"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:hint="Write Greeting"
        android:textSize="15sp" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/edit_view1"
        android:background="@android:color/transparent"
        android:src="@drawable/e301" />
</LinearLayout>

Using weight then you must have width = 0dp(horizontal) and heigth = 0dp(vertically)

  1. Use weights: you didn't define weightSum of layout so your weights are now useless. Set parent layout's weightSum to 6 (in order to use your values) or 100 and set children's weights as percents. And change children views' widths to 0dp .

  2. [optional] Set button's width to match_parent so it'll take all remaining space.

  3. In LinearLayout layout_toRightOf, layout_alignParentRight, layout_alignParentLeft are redundant. Use gravity for children views instead. (for Button it'll be `gravity="right").

Use the below coding. Tell me if you want better alignment. I have used default default image for alignment. If you want better than this, then give buttom image also.

<LinearLayout
        android:id="@+id/layout_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <TextView
                android:id="@+id/edit_view1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:hint="Write Greeting dfdsfdsfsdfadsfdfadf"
                android:textSize="15sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3" >

            <ImageButton
                android:id="@+id/imageButton1"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_launcher" />
        </RelativeLayout>
    </LinearLayout>

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