简体   繁体   中英

How to align toggle button with image button

I am using a toggle button for selecting favorites and I've got several other image buttons in the same row. However, because toggle button doesn't behave similar to image button, my icons aren't aligning properly.

Also, I'm setting the image for the toggle button programmatically, because it needs to have a changed button based on what the user selected. I do that programatically like this:

  if (holder.favButton.isChecked())
     holder.favButton.setBackgroundDrawable(context.getResources().
          getDrawable(R.drawable.star_fill));
  else
     holder.favButton.setBackgroundDrawable(context.getResources().
          getDrawable(R.drawable.star_empty));

Please see the screenshot. Both the icons in this image are 24 x 24

在此处输入图片说明

This is my layout:

I've tried several combinations but none seem to work

This is my layout:

<LinearLayout
    android:id="@+id/share_c1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ToggleButton android:id="@+id/fav_rec"
        android:layout_width="35px"
        android:layout_height="35px"
        android:background="#ffffff"
        android:focusable="false"
        android:layout_marginRight="10dp"
        android:paddingBottom="15px"
        android:textOn="" android:textOff="" android:layout_alignParentLeft="true"
        />


    <ImageButton
        android:id="@+id/edit"
        android:layout_width="35px"
        android:layout_height="35px"
        android:background="#ffffff"
        android:focusable="false"
        android:layout_marginRight="10dp"
        android:layout_alignParentLeft="true"
        android:paddingBottom="5px"
        android:src="@drawable/pencil_1"/>
  </LinearLayout>

Remove paddingBottom and layout_alignParentLeft on both the ToggleButton and ImageButton .

Then add android:gravity="center_vertical" to your parenting LinearLayout , and android:gravity="center" to your ToggleButton and ImageButton .

// try this way,hope this will help you...

<LinearLayout
        android:id="@+id/share_c1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ToggleButton
            android:id="@+id/fav_rec"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="#ffffff"
            android:focusable="false"
            android:layout_marginRight="10dp"
            android:paddingBottom="15dp"
            android:textOn=""
            android:textOff=""/>


        <ImageButton
            android:id="@+id/edit"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="#ffffff"
            android:focusable="false"
            android:layout_marginRight="10dp"
            android:paddingBottom="5dp"
            android:src="@drawable/ic_launcher"/>
    </LinearLayout>

Note : try use dp or dip insted of px in layout.

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