简体   繁体   English

如何将切换按钮与图像按钮对齐

[英]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 该图像中的两个图标均为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 . 删除ToggleButtonImageButton上的paddingBottomlayout_alignParentLeft

Then add android:gravity="center_vertical" to your parenting LinearLayout , and android:gravity="center" to your ToggleButton and ImageButton . 然后将android:gravity="center_vertical"到您的父母LinearLayout ,并将android:gravity="center"到您的ToggleButtonImageButton

// 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM