Domanda

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

enter image description here

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>
È stato utile?

Soluzione

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.

Altri suggerimenti

// 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.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top